OnClickListener ListView中填充了的CursorAdapter [英] OnClickListener in Listview populated with a CursorAdapter

查看:200
本文介绍了OnClickListener ListView中填充了的CursorAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在每行2个按钮的列表视图。
我使用的是cursoradpater填充列表。

I have a list view with 2 buttons on each row. I am using a cursoradpater to populate the list.

我还使用上NewView的)观点持有者模式()bindview(

I am also using the view holder pattern on newview() bindview().

我的问题是:
我在哪里可以放的按钮知道该按钮的动作是从列表项目本身的作用不同的clicklisteners?
我保持onListItemClick?

My questions are: where do i put the clicklisteners for the buttons knowing that the action for the button is different from the action of the list item itself? Do i keep the onListItemClick ?

推荐答案

您不需要 onListItemClick

您可以尝试每个按钮的事件在适配器结合

You can try binding for each of your button an event in the adapter

final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });

但也许这不会对列表项工作,所以你需要一个新的形式给出的是在的按钮文档

然而,而不是在您的活动施加OnClickListener的按钮,就可以将方法分配给你的XML布局按钮,采用了android:onclick属性。例如:

However, instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:

<Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="selfDestruct" />

现在,当用户点击该按钮时,Android系统会调用活动的selfDestruct(查看)方法。为了使这项工作,方法必须公开,并接受一个View作为其唯一的参数。例如:

Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:

 public void selfDestruct(View view) {
     // Kabloey
 }

传递到方法的视图是被点击的小部件的引用。

The View passed into the method is a reference to the widget that was clicked.

这篇关于OnClickListener ListView中填充了的CursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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