Emilla
Emilla

Reputation: 494

Handling button event in each row of Listview issue

I am new to android and am trying to develop a new android app. But I am struggling to siolve one of the problems in my project.

I am using a listview extended from baseadapter and need to add a button in each row of thelistview. When I click on the button in any row of the listview, I want that it should be removed. However when I do so, some of the other buttons also get removed in the listview.

How can I solve this problem? Thank you..

Upvotes: 2

Views: 2048

Answers (2)

Soumyadip Das
Soumyadip Das

Reputation: 1791

I faced same type of problem. ListView's setOnItemClickListener not works if you add item like a button on every listView item. Solution is use onClick in the list Item layout(which you use in custom adapter file) as

<ImageButton
        android:id="@+id/my_delete"
        android:onClick="onDeleteButtonClickListener" 
        ... and so on />

where onDeleteButtonClickListener is a method in the activity where you set the adapter in listview.

public void onDeleteButtonClickListener(View v) {
// your code
}

here listItem means the individual row item of a ListView


Helpful Link: Button in ListView item

Upvotes: 0

Alehar
Alehar

Reputation: 639

You have an adapter, activity and some sort of data source

In your adapter you attach some data to buttons to be able to tell one from another:

public class ExpAdapter extends ListAdapter {

    @Override
    public View getView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
                /* SOME CODE HERE*/
        convertViewButton.setTag(buttonId);
        return convertView;
    }
                /* SOME CODE HERE*/
}

in your activity you mark button id as the one to be hidden:

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            storageOfHiddenButtonsIds.add((Long)arg1.getTag());
        }};

and then ListAdapter changes like this:

@Override
public View getView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
            /* SOME CODE HERE*/

    convertViewButton.setTag(buttonId);

    if(storageOfHiddenButtonsIds.contains(buttonId))
    {
      convertViewButton.setVisiblity(View.GONE);
    }
    return convertView;
}

and when you want your adatper to change you, don't forget to call

this.expAdapterAllTaks.notifyDataSetChanged();

Sorry for any errors in my code, but i just wanted to give you an idea.

Upvotes: 1

Related Questions