Delete group in Expandable List

后端 未结 2 1302
清歌不尽
清歌不尽 2020-12-10 09:47

I am trying to delete Group when selected in my Expandable List and having the list refreshed after the delete occurred.

I took the source code from

2条回答
  •  -上瘾入骨i
    2020-12-10 10:12

    Add the functionality to your Adapter to remove an Item:

        public void removeGroup(int group) {
            //TODO: Remove the according group. Dont forget to remove the children aswell!
            Log.v("Adapter", "Removing group"+group);
            notifyDataSetChanged();
        } 
    
        public void removeChild(int group, int child) {
            //TODO: Remove the according child
            Log.v("Adapter", "Removing child "+child+" in group "+group);
            notifyDataSetChanged();
        }
    

    Make sure the new methods are accessible to you by changing:

    ExpandableListAdapter mAdapter;

    to

    MyExpandableListAdapter mAdapter;

    Call the methods when needed:

     @Override
     public boolean onContextItemSelected(MenuItem item) {
        ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
        String title = ((TextView) info.targetView).getText().toString();
    
        int type = ExpandableListView.getPackedPositionType(info.packedPosition);
        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
            int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); 
            mAdapter.removeChild(groupPos, childPos);
            return true;
        } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); 
            mAdapter.removeGroup(groupPos);
            return true;
        }
    
        return false;
    }
    

    So, hope that helps. Cheers

提交回复
热议问题