安卓:事件侦听器添加到每个项目在ListView [英] Android: Add event listeners to every item in a ListView

查看:92
本文介绍了安卓:事件侦听器添加到每个项目在ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView的Andr​​oid应用程序,并在列表中的每一行都有一个TextView和一个按钮。我想要做的是一个OnClickListener添加到ListView的每个按钮,但我无法弄清楚如何获得某种参照每个按钮......任何人都可以请给我一个提示?

下面是我的XML绑定到该ListAdapter:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT>
<的TextView
    机器人:ID =@ + ID / row_text
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:TEXTSIZE =18sp>
< / TextView的>
<按钮
    机器人:ID =@ + ID / row_button
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentRight =真/>
< / RelativeLayout的>
 

和我想这样的事情,但它不工作:

  SimpleCursorAdapter行=新SimpleCursorAdapter(这一点,R.layout.row_layout,光标,从,到);
setListAdapter(行);
Button按钮=(按钮)getListAdapter()getView(0,空,getListAdapter())findViewById(R.id.row_button)。
button.setOnClickListener(新View.OnClickListener(){
    @覆盖公共无效的onClick(视图v){
        Log.i(TAG,点击);
    }
});
 

解决方案

这是不可能使用 SimpleCursorAdapter ...你必须创建自己的适配器。如果您不想编写自定义适配器,至少设法加强与新功能的SimpleCursorAdapter。例如:

 公共类YourAdapter扩展SimpleCursorAdapter {

    公共YourAdapter(上下文的背景下,INT布局,光标C,的String []从,INT []键){
        超(背景下,布局,C,从,到);
    }

    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看查看= super.getView(位置,convertView,父母);
        Button按钮=(按钮)view.findViewById(R.id.row_button);
        button.setOnClickListener(新View.OnClickListener(){
            @覆盖公共无效的onClick(视图v){
                Log.i(TAG,点击);
            }
        });
        返回查看;
    }
}
 

然后,你可以这样做:

  SimpleCursorAdapter行=新YourAdapter(这一点,R.layout.row_layout,光标,从,到);
setListAdapter(行);
 

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

解决方案

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

这篇关于安卓:事件侦听器添加到每个项目在ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆