Android:为 ListView 中的每个项目添加事件侦听器

Android: Add event listeners to every item in a ListView(Android:为 ListView 中的每个项目添加事件侦听器)
本文介绍了Android:为 ListView 中的每个项目添加事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 ListView 的 Android 应用,列表中的每一行都有一个 TextView 和一个 Button.我想要做的是为 ListView 中的每个 Button 添加一个 OnClickListener,但我不知道如何获得对每个 Button 的某种引用......谁能给我一个提示?

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?

这是绑定到 ListAdapter 的 XML:

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");
    }
}); 

推荐答案

不可能使用 SimpleCursorAdapter... 你必须创建自己的适配器.如果您不想编写自定义适配器,至少尝试使用新功能增强 SimpleCursorAdapter.例如:

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

那么,你可以这样做:

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

这篇关于Android:为 ListView 中的每个项目添加事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)