Delete group in Expandable List

后端 未结 2 1301
清歌不尽
清歌不尽 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条回答
  • 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

    0 讨论(0)
  • 2020-12-10 10:15

    Answer for the second question (Add rows to a child & datastructure)

    For the Datastructure:

    public class GroupItem {
     private String mItemText;
     private List<ChildItem> mChildItems=new ArrayList<ChildItem>();    
    
     public GroupItem(String itemText) {
      mItemText=itemText;
     }
    
     public String getItemText() {
      return mItemText;
     }
    
     public ChildItem getChild(int childPosition) {
      return mChildItems.get(childPosition);
     }
    
     public void addChild(ChildItem childItem) {
      return mChildItems.add(childItem)
     }
    
     public void removeChild(int childPosition) {
      return mChildItems.remove(childPosition);
     }
    
    }
    
    public class ChildItem {
     private String mItemText;
     public ChildItem(itemText) {
      mItemText=itemText;
     }
    
     public String getItemText() {
      return mItemText;
     }
    }
    

    Now to set it up you would do something like:

    List<GroupItem> items = new ArrayList<GroupItem>();
    
    GroupItem item1 = new GroupItem("This is group 1");
    item1.addChild(new ChildItem("This is a child item of group 1"));
    item1.addChild(new ChildItem("This is another child item of group 1"));
    

    and so on...

    Then, in the Adapter you would need to return the appropriate data.

    For your question concerning rows: In your Google example, they return a TextView. You can however make your own Layout with whatever content you like. For example:

    <ImageView android:id="@+id/RowIcon" 
      android:layout_width="56px" 
      android:layout_height="56px" 
      android:layout_marginLeft="12px" 
      android:layout_marginTop="2px">
    </ImageView>
    <TextView android:id="@+id/RowTextView"
         android:paddingLeft="10px"
         android:paddingTop="10px"       
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" android:textColor="#666666" android:textSize="14px"/>
    
    
    </LinearLayout>
    

    And then use this Layout in your Adapter. Instead of returning, say, a TextView, you'll just return this as a View:

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {
    
    LayoutInflater mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = mInflater.inflate(YOUR XML FILE FROM ABOVE, null);
        ImageView rowIcon = (ImageView)rowView.findViewById(R.id.RowIcon);
        iconType.setBackgroundResource(AN IMAGE HERE);
        TextView rowText =(TextView)rowView.findViewById(R.id.RowTextView);
        textAddress.setText(items.get(groupPosition).getChild(childPosition));
    
        return rowView;
     }
    

    So, i think that should get you going.

    Also, please accept my answers if they satisfy you.

    Cheers.

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