setOnItemClickListener事件侦听器的问题

时间:2012-12-07 00:39:18

标签: android

我正在尝试在用户点击文本视图中的值时打开新活动。这里是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,但这只会产生许多错误,而Google搜索时找不到任何结果。

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

由于 埃里克

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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()OnCreate()中:      lvRegionsPriorities.setOnItemClickListener(本);

其他地方覆盖:

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);         
     }
}