Remove child from an ExpandedListView

2

Hello, I would like to know how to remove a subitem from an ExpandedListView, in other words remove the item from the adapter, the adapter receives a Map<Categoria, List<SubCategoria>> and then give a notifyDataSetChanged() .

Fragment from the list

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_subcategoria_despesa, container, false);
    mListView = (ExpandableListView) v.findViewById(R.id.listDes);
    refreshLista();
    return v;
}

@Override
public void onResume() {
    super.onResume();
}

private void refreshLista() {
    CategoriaDAO cDAO = new CategoriaDAO(getContext());
    SubCategoriaDAO sDAO = new SubCategoriaDAO(getContext());
    List<Categoria> cList = cDAO.getLista(1);
    collection = new LinkedHashMap<>();
    for (Categoria c : cList){
        List<SubCategoria> sList = sDAO.getLista(c.getId());
        collection.put(c, sList);
    }
    SubcategoriaExpandableAdapter adapter = new SubcategoriaExpandableAdapter(getContext(), cList, collection);
    mListView.setAdapter(adapter);
}

List Adapter

public class SubcategoriaExpandableAdapter extends BaseExpandableListAdapter {
private Context mContext;
private Map<Categoria, List<SubCategoria>> mCollection;
private List<Categoria> mList;
private LayoutInflater inflater;


public SubcategoriaExpandableAdapter(Context context, List<Categoria> list, Map<Categoria, List<SubCategoria>> collection) {
    this.mContext = context;
    this.mCollection = collection;
    this.mList = list;
    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return mCollection.get(mList.get(groupPosition)).get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    final SubCategoria subCategoria = (SubCategoria) getChild(groupPosition, childPosition);

    ViewHolderItem holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.subcategoria_row, null);
        holder = new ViewHolderItem();
        convertView.setTag(holder);
        holder.tvItem = (TextView) convertView.findViewById(R.id.tvSubCat);
        holder.idItem = (TextView) convertView.findViewById(R.id.id);
        holder.btItem = (ImageButton) convertView.findViewById(R.id.opcsubcat);

    }else{
        holder = (ViewHolderItem) convertView.getTag();
    }
    holder.tvItem.setText(subCategoria.getNome());
    holder.idItem.setText(String.valueOf(subCategoria.getId()));
    final View finalConvertView = convertView;
    holder.btItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popupMenu = new PopupMenu(mContext, v);
            popupMenu.getMenuInflater().inflate(R.menu.subcat_menu, popupMenu.getMenu());
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.editarSub:
                            Intent intent = new Intent(mContext, EditaSubcategoriaActivity.class);
                            intent.putExtra("id", subCategoria.getId());
                            intent.putExtra("idcat", subCategoria.getIdCategoria());
                            mContext.startActivity(intent);
                            break;
                        case R.id.apagarSub:
                            AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
                            ab.setTitle("Apagar subcategoria?");
                            ab.setMessage("Todas os lançamentos dessa subcategoria serão apagados.");
                            ab.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    SubCategoriaDAO dao = new SubCategoriaDAO(mContext);
                                    LancamentoDAO dao1 = new LancamentoDAO(mContext);
                                    dao1.deleteAllBySubcategoria(subCategoria.getId());
                                    dao.deletar(subCategoria);
                                    dialog.dismiss();
                                }
                            });
                            ab.setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                            ab.show();
                            break;
                    }
                    return false;
                }
            });
            popupMenu.show();
        }
    });

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return mCollection.get(mList.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return mList.get(groupPosition);
}

@Override
public int getGroupCount() {
    return mList.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    final Categoria categoria = (Categoria) getGroup(groupPosition);
    ViewHolderGroup holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.categoria_row, null);
        holder = new ViewHolderGroup();
        convertView.setTag(holder);
        holder.tvGroup = (TextView) convertView.findViewById(R.id.tvCat);
        holder.idGroup = (TextView) convertView.findViewById(R.id.idc);
    }else{
        holder = (ViewHolderGroup) convertView.getTag();
    }
    holder.tvGroup.setText(categoria.getNome());
    holder.idGroup.setText(String.valueOf(categoria.getId()));


    ExpandableListView listView = (ExpandableListView) parent;
    listView.expandGroup(groupPosition);
    listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            parent.expandGroup(groupPosition);
            Intent i = new Intent(mContext, EditaSubcategoriaActivity.class);
            i.putExtra("idcat", categoria.getId());
            mContext.startActivity(i);
            return true;
        }
    });
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i("LOGE", position + "");
        }
    });

    return convertView;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

class ViewHolderGroup {
    TextView tvGroup;
    TextView idGroup;
}

class ViewHolderItem {
    TextView tvItem;
    TextView idItem;
    ImageButton btItem;
}

When I click delete I delete the subcategory of the database, but I did not find a way to delete it from the adapter in order to update expandedlistview.

Thank you in advance.

    
asked by anonymous 04.04.2016 / 22:02

1 answer

2

Include these two methods in the SubcategoryExpandableAdapter class.

public void removerSubCategorias(int groupPosition) {
    mCollection.get(mList.get(groupPosition)).clear();
    notifyDataSetChanged();
}

public void removerSubCategoria(int groupPosition, int childPosition) {
    mCollection.get(mList.get(groupPosition)).remove(childPosition);
    notifyDataSetChanged();
}

If you want to delete the entire List<SubCategoria> , getting to Category without items (sub-categories) use removerSubCategorias(groupPosition); .

If you want to delete an item (subcategory) from List<SubCategoria> , getting Category with one less item (subcategory) use removerSubCategoria(groupPosition, childPosition);

    
05.04.2016 / 13:09