Search code examples
androidadapterexpandablelistview

Remove items from ExpandableList adapter


I have the following problem:

I have implemented an ExpandableListView that shows me a series of records from a database. Next to each item there is a button to delete that item.

My problem is that the method that deletes the item from the ExpandableList works fine, but I want it to refresh the ExpandableList when it finishes deleting and show me the records without that item again.

This is my method from the adapter:

    public class BaseExpandableListViasAdapterde extends BaseExpandableListAdapter {
    
(...)
     public BaseExpandableListViasAdapterde(ArrayList<String> listCategoria, Map<String, ArrayList<String>> mapChild, Map<String, ArrayList<String>> mapChildNomVias, Context context) {
            this.listCategoria = listCategoria;
            this.mapChild = mapChild;
            this.mapChildNomVias = mapChildNomVias;
            this.context = context;
        }

    (...)

    @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
    
            final String itemCod = (String)getChild(groupPosition, childPosition);
            final String itemNom = (String)getChild2(groupPosition, childPosition);
    
            convertView = LayoutInflater.from(context).inflate(R.layout.filter_child_layout_vias, null);
    
            TextView tvChildCodigoVia = convertView.findViewById(R.id.txtViasCod);
            TextView tvChildNombreVia = convertView.findViewById(R.id.txtViasnombre);
    
            ImageView buttonTrash= convertView.findViewById(R.id.imgBorrarVia);
    
            buttonTrash.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    String codVia = (String) getChild(groupPosition, childPosition);
    
                    borraViasBD(codVia); //This method delete the item in the database. Works fine.
    
                    //Try this, but don't work
                    listCategoria.remove(groupPosition);
                    notifyDataSetChanged();
    
                }
            });
    
            tvChildCodigoVia.setText(itemCod);
            tvChildNombreVia.setText(itemNom);
            return convertView;
    }
    
(...)
    }

Any idea how to refresh the ExpandableList? Thanks you!


Solution

  • Try this:

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    
        String itemCod = (String)getChild(groupPosition, childPosition);
        String itemNom = (String)getChild2(groupPosition, childPosition);
    
        convertView = LayoutInflater.from(context).inflate(R.layout.filter_child_layout_vias, null);
    
        TextView tvChildCodigoVia = convertView.findViewById(R.id.txtViasCod);
        TextView tvChildNombreVia = convertView.findViewById(R.id.txtViasnombre);
    
        ImageView buttonTrash= convertView.findViewById(R.id.imgBorrarVia);
    
        buttonTrash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                String codVia = (String) getChild(groupPosition, childPosition);
    
                borraViasBD(codVia); //This method delete the item in the database. Works fine.
    
                //Try this
                ArrayList<String> listCod = mapChild.get((String)getgroup(groupPosition));
                ArrayList<String> listNom = mapChildNomVias.get((String)getgroup(groupPosition));
                listCod.remove(childPosition); // Updated
                listNom.remove(childPosition); // Updated
                notifyDataSetChanged();
            }
        });
    
        tvChildCodigoVia.setText(itemCod);
        tvChildNombreVia.setText(itemNom);
        return convertView;
    }