开发者

OnClickListener in Listview populated with a CursorAdapter

I have a list view with 2 buttons on e开发者_运维技巧ach row. I am using a cursoradpater to populate the list.

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

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 ?


You do not need the 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
             }
         });

but probably this won't work on the list item, so you need a new aproach as is this described in the button documentation.

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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜