Izak
Izak

Reputation: 991

How to know which button in which item was clicked

I have a delete button in each item in a listview that gives the user a chance to delete an item they created. I need to know though which button was clicked in which item- how do i get the item that a user clicked a button in? (i use a custom adapter)?Thanks

Upvotes: 0

Views: 69

Answers (3)

Genevieve
Genevieve

Reputation: 450

not the cleanest method but you can set your button onclick listener in your customadapter getview method

  @Override
public View getView(int position, View convertView, ViewGroup parent) {

   ...

    Button btnDelete= (Button) convertView.findViewById(R.id.btnDelete);

       btnDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //execute your codes here

        }
    });

}

Upvotes: 1

Yushi
Yushi

Reputation: 93

You can use setTag(Object tag) to set the item as tag of the button when you create each cell of the listview. And when the button got clicked use getTag () to get the object (you need to cast it to your item type).

Then you can do whatever you want with your item.

Upvotes: 0

Graeme
Graeme

Reputation: 1117

Set a tag on the item. You can use an identifier of the tag (even an iterated value) to identify it

Upvotes: 0

Related Questions