Handling button event in each row of Listview issue

后端 未结 2 1742
滥情空心
滥情空心 2021-01-23 02:43

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

相关标签:
2条回答
  • 2021-01-23 03:06

    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.

    0 讨论(0)
  • 2021-01-23 03:14

    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

    0 讨论(0)
提交回复
热议问题