Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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:

publicclassExpAdapterextendsListAdapter {

    @Overridepublic 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:

@Overridepublic 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.

Solution 2:

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.

publicvoidonDeleteButtonClickListener(View v) {
// your code
}

here listItem means the individual row item of a ListView


Helpful Link:Button in ListView item

Post a Comment for "Handling Button Event In Each Row Of Listview Issue"