如何将setOnItemClickListener设置为listview?

时间:2013-09-25 12:05:37

标签: android listview android-listview

我尝试将setOnItemClickListener用于自定义列表视图。我将适配器设置为

final ListView searchlist = (ListView) findViewById(R.id.prodlist);
String[] from = new String[] {"rowid", "col_1", "col_2","col_3"};
int[] to = new int[] { R.id.checkBox1, R.id.editText1, R.id.editText2, R.id.editText3}; 

 //added data to the components using for loop.

 SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.prod_view,from, to);
 list.setAdapter(adapter); 
      list.setOnItemClickListener(new OnItemClickListener()
            {
                    public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
                    {
                    Toast.makeText(Prod.this,"" + position, Toast.LENGTH_SHORT).show(); 
                    }
          }); 

但是当我点击列表视图中的复选框时,它不显示toast信息。如果在列表视图中选中复选框,我如何获得该位置?

3 个答案:

答案 0 :(得分:0)

然后实施checkBox点击事件。为什么要实现ListView单击事件。使用setTag()和getTag(),正确获取checkBox id,然后实现checkBox click事件。

答案 1 :(得分:0)

您需要将以下属性添加到项目的布局中:

android:descendantFocusability="blocksDescendants"

否则项目内的复选框将处理该事件。这将允许您在列表视图中处理事件。

E.g。如果您的项目布局以

开头
<RelativeLayout        
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants" >

... the rest of the item

</RelativeLayout>

答案 2 :(得分:0)

试试这个,使用setOnCheckedChangeListener

   mHolder.mcheckbox
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(
                                CompoundButton buttonView, boolean isChecked) {
                //Do your stuff here
                        }
                    });
相关问题