ListView单击侦听器不起作用

时间:2013-07-24 18:21:26

标签: android listview click simplecursoradapter

我在活动中使用自定义SimpleCursorAdapter的ListView:

public class MyListActivity extends Activity { ... }

mCursorAdapter = new MyCursorAdapter(mContext,
     R.layout.listview_item01, mCursor, from, to);
mainLayout = (RelativeLayout) findViewById(R.id.main_layout);

mListView = new ListView(mContext);
mListView.setAdapter(mCursorAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> arg0, View view,
          int position, long arg3) {
          Toast.makeText(getApplicationContext(),
               "You Selected Item " + Integer.toString(position),
               Toast.LENGTH_LONG).show();
          Log.v("onItemClick", "CLICK!");
    }
});
mainLayout.addView(mListView);

我的简单适配器:

private class MyCursorAdapter extends SimpleCursorAdapter {

     public MyCursorAdapter(Context context, int layout, Cursor c,
          String[] from, int[] to) {
          super(context, layout, c, from, to);
     }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
          View view = super.getView(position, convertView, parent);
          if (position % 2 == 0) {
               view.setBackgroundColor(Color.rgb(238, 233, 233));
          } else {
               view.setBackgroundColor(Color.rgb(255, 255, 255));
          }
          return view;
     }
}

我的列表视图项目:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RadioButton
        android:id="@+id/item_radiobutton01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />
    <TextView
        android:id="@+id/item_textview01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/item_radiobutton01"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/item_radiobutton01"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

我所看到的:

enter image description here

我的ListView看起来很好,但如果我点击任何项目,那么没什么好玩的。为什么?请帮帮我

1 个答案:

答案 0 :(得分:3)

我认为这是因为你的布局项目。尝试使用以下方法禁用它们的可点击性:

    android:focusable="false"
    android:focusableInTouchMode="false"

我认为当您点击某个项目时,您实际上并没有点击该行,而是单击了TextView或RadioButton;您可以通过为它们编写事件处理程序来检查这一点,并将Log放在这些事件处理程序中。

有关这些属性的更多信息,请查看android开发者网站中的this document