Android: Add event listeners to every item in a ListView

前提是你 提交于 2019-11-28 09:27:54

问题


I have an Android app with a ListView, and each row in the list has a TextView and a Button. What I want to do is add an OnClickListener to each Button in the ListView, but I can't figure out how to get some sort of reference to every Button... Can anyone please give me a hint?

Here's my XML that's bound to the ListAdapter:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView 
    android:id="@+id/row_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:textSize="18sp">
</TextView>
<Button
    android:id="@+id/row_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true" />
</RelativeLayout>

And I tried something like this, but it doesn't work:

SimpleCursorAdapter rows = new SimpleCursorAdapter(this, R.layout.row_layout, cursor, from, to);
setListAdapter(rows);
Button button = (Button) getListAdapter().getView(0, null, getListAdapter()).findViewById(R.id.row_button);
button.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        Log.i(TAG, "clicked");
    }
}); 

回答1:


It's not possible using SimpleCursorAdapter... you will have to create your own adapter. If you don't want to write a custom Adapter, at least try to enhance the SimpleCursorAdapter with new capabilities. For instance:

public class YourAdapter extends SimpleCursorAdapter{

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

    public View getView(int position, View convertView, ViewGroup parent){
        View view = super.getView(position, convertView, parent);
        Button button = (Button)view.findViewById(R.id.row_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                Log.i(TAG, "clicked");
            }
        }); 
        return view;
    }
}

Then, you can do:

SimpleCursorAdapter rows = new YourAdapter(this, R.layout.row_layout, cursor, from, to);
setListAdapter(rows);



回答2:


Regarding Cristian's answer, one thing I discovered is that getView is called many times, not just when the view is created. So, you will be executing your getView code more frequently than you might think.

If the attributes (e.g. the OnClick listener) you are adding are invariant across all elements in the list, you can override newView instead. It will be called exactly once for each displayed row in the ListView. However, be warned that ListView recycles views, so as you scroll, the ones that drop off one end of the view are reused on the other, but with new data from the cursor. Again, as long as your attributes are invariant, this will work great.



来源:https://stackoverflow.com/questions/3851802/android-add-event-listeners-to-every-item-in-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!