0

当用户单击文本视图中的值时,我试图打开一个新活动。这里的 onCreate 方法:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get change in selected projects group & show group accordingly
    RadioGroup rgProjectGroupsToLoad = (RadioGroup) findViewById(R.id.rgProjectGroupsToLoad);
    rgProjectGroupsToLoad.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup rGroup, int checkedId)
        {
            // Get the vocab needed to provide options
            Resources res = getResources();
            String[] region_values = res.getStringArray(R.array.regions);
            String[] priority_values = res.getStringArray(R.array.priorities);
            String[] curr_values = region_values;

            TextView tvGroupingHeader = (TextView) findViewById(R.id.listRegionsPrioritiesHeader);

            RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId);
            boolean isChecked = checkedRadioButton.isChecked();
            if (isChecked)
            {
                String selected_group = (String) checkedRadioButton.getText();
                Log.w("myApp", "Project Group selected: " + selected_group);
                tvGroupingHeader.setText(selected_group);
                if (selected_group.equals("Geographic Regions"))
                {
                    curr_values = region_values;
                }
                else
                {
                    curr_values = priority_values;
                }
                // Project Group Adapter for ListView
                ArrayAdapter<String> proj_group_adapter = new ArrayAdapter<String>(
                        getBaseContext(),
                        android.R.layout.simple_list_item_1, 
                        android.R.id.text1, 
                        curr_values
                );
                ListView lvRegionsPriorities = (ListView) findViewById(R.id.listRegionsPriorities);
                lvRegionsPriorities.setAdapter(proj_group_adapter); 

                // Show hidden elements
                TextView tv = (TextView) findViewById(R.id.listRegionsPrioritiesHeader);
                tv.setVisibility(View.VISIBLE);
                ListView lv = (ListView) findViewById(R.id.listRegionsPriorities);
                lv.setVisibility(View.VISIBLE);
            }
        }
    });
}

我试过设置lvRegionsPriorities.setOnItemClickListener,但这只会产生很多错误,我在谷歌搜索时找不到任何结果。

那么,有人可以在这里帮忙吗?如何正确设置事件监听器?

谢谢埃里克

4

2 回答 2

0

如果TextView在您的活动中:

TextView tv = (TextView) findViewById(...);
tv.setOnClickListener(new OnClickListener{
    public void onClick(View v){
    Intent i = new Intent(CurrentActivity.this, NextActivity.class);
    startActivity(i);
    }
})

如果您想在点击给定项目时打开活动ListView

让您的活动实施OnItemClickListener() In OnCreate(): lvRegionsPriorities.setOnItemClickListener(this);

在其他地方覆盖:

public void onItemClick(View v, int position, long id){
     if (position == xx){ // check your condition here
         Intent i = new Intent(CurrentActivity.this, NextActivity.class);
         startActivity(i);         
     }
}
于 2012-12-07T02:13:26.030 回答
0

我没有看到您尝试开始新活动的任何内容。我确实看到您试图在同一活动中显示隐藏的视图。您使用 Intents 开始一项新活动。如果问题是当您单击它时您的视图没有出现,则可以尝试将它们启动为可见并查看它们是否显示。它可能与单选按钮无关。

于 2012-12-07T00:54:13.693 回答