0
我有一個名爲FavListActivity的活動,它包含一個listview。如何將onClickListener添加到由自定義光標適配器填充的listview中的imageview中?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xkcdreader.FavListActivity"
tools:ignore="MergeRootFrame" >
<ListView
android:id="@+id/favL"
android:layout_width="match_parent"
android:layout_height="452dp"
android:layout_alignParentBottom="true"
android:layout_below="@+id/headingTV" >
</ListView>
<TextView
android:id="@+id/headingTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="8dp"
android:text="Favourites"
android:textSize="25dp" />
</RelativeLayout>
此列表的每一行都是由此xml文件樣式。每行有2個文字瀏覽和一個按鈕。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/listTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:layout_toLeftOf="@+id/trashIV"
android:focusable="false"
android:gravity="center_vertical"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:text="Example application"
android:textSize="26sp" />
<ImageView
android:id="@+id/trashIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/listTitle"
android:layout_alignParentRight="true"
android:src="@drawable/trash"
android:onClick="trash"/>
</RelativeLayout>
此列表視圖中有一個名爲CAdapter自定義光標適配器:
public class CAdapter extends CursorAdapter {
public CAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView titleTV = (TextView) view.findViewById(R.id.listTitle);
titleTV.setText("#"+cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1)))+" "+
cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.row_item_layout, parent, false);
return retView;
}
}
我的問題是如何設置的onClick偵聽一行中的每個ImageView的。 Link to my ListView