Remove child from an Expandedlistview

Asked

Viewed 54 times

2

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

Fragment of 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;
}

By clicking delete I delete the subcategory of the database, but I couldn’t find a way to delete it from Adapter to update the expandedlistview.

Thanks in advance.

1 answer

2


Include these two methods in the class Subcategoriaexpandabled pter.

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 eliminate the List<SubCategoria> whole, getting the Category no items(sub-categories) use removerSubCategorias(groupPosition);.

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

  • 1

    Very good, worked as I wanted, thanks @ramaral.

  • and if I update an item, as I do?

  • Depends. Ask new question and give details of how/where you are making this change.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.