简体   繁体   English

如何将onClickListener添加到由自定义Cursor适配器填充的列表视图中的图像视图?

[英]How to add onClickListener to a imageview in a listview populated by custom Cursor adapter?

I have an activity called FavListActivity which contains a listview. 我有一个名为FavListActivity的活动,其中包含一个列表视图。

<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>

Each row of this list is styled by this xml file. 此列表的每一行均由该xml文件设置样式。 Each row has 2 textviews and a button. 每行有2个textviews和一个按钮。

<?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>

This listview has a custom cursor adapter called CAdapter: 此列表视图具有一个称为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;
    }
}

My question is how to setup onClick listener for each imageview in a row. 我的问题是如何为一行中的每个imageview设置onClick侦听器。 Link to my ListView 链接到我的ListView

You should do it In your Adapter not in Activity. 您应该在适配器中而不在活动中执行此操作。

yourImg.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // things you want to do 
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM