onListItemClick在自定义ListView中

时间:2011-05-19 17:33:06

标签: android listview

我正在使用自定义ListView,每行有几个TextView对象。在onListItemClick中,我想获得被点击的实际TextView,但我只是得到包含两个TextView的LinearLayout视图。

class CustomList extends ListActivity {
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custom_list_view);
    ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.custom_list_item, R.id.label, data);
    setListAdapter(aa);
  }

  public void onListItemClick(ListView lv, View v, int index, long id){
    // I would like to access the TextView that was clicked here
    // v = LinearLayout, not the TextView that was clicked
  }
}

下面是res / custom_list_item.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
<TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/label"
        android:textSize="30sp">
</TextView>
<TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/delete_text"
        android:textSize="30sp"
        android:text="X">
</TextView>
</LinearLayout>

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

您必须为onClickListener项定义TextView。目前,您的点击监听器仅适用于您的CustomList,而不适用于您的TextView项。