2

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

Emilla
  • 484
  • 1
  • 10
  • 23
  • 2
    Please post the code of how you are removing the buttons. – Comic Sans MS Lover Jul 06 '12 at 13:42
  • really how will you get a help if you post a code? – Zamani Jul 06 '12 at 13:44
  • If you have time, I think it's worth to watch this ‒ [Google I/O 2010 - The world of ListView](http://www.youtube.com/watch?v=wDBM6wVEO70). –  Jul 06 '12 at 14:11
  • This is what i have done so far for getView() method... I really have no idea how i will do... i i knew what makes buttons in each row of listview specific i would solve my problem. – Emilla Jul 06 '12 at 14:44

2 Answers2

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:

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.

Alehar
  • 639
  • 5
  • 16
0

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

Soumyadip Das
  • 1,781
  • 3
  • 16
  • 34
  • But it seams to me all buttons in each row of listview has same id. Because when i try to get v.getId() it returns same answer to me. i really do not know how i am going to handle this. Could you please be more clear about your solution? – Emilla Jul 06 '12 at 14:08
  • try this:http://androidforbeginners.blogspot.in/2010/03/clicking-buttons-in-listview-row.html or you may search the internet for thid type of topic... it probably will help you. **I get solved by this link** :) – Soumyadip Das Jul 06 '12 at 14:17