简体   繁体   中英

how to remove the child from the list in expandablelistview after clicked?

So I have an ExpandableListView. After I clicked the child, it will delete the data from mysql table. After the data is being deleted from the table I want it to be removed from the list.

I tried using remove() but still not working. Here's my code

exKonsumsi = (ExpandableListView) findViewById(R.id.EVkonsumsi_user);
        exKonsumsi.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        prepareListMenuPagi();
        exKonsumsiAdapter = new com.ta.helper.ExpandableListAdapterKonsumsi(
                this, listDataHeaderDaftarAwal, listDataChildDaftarAwal);
        exKonsumsi.setAdapter(exKonsumsiAdapter);
        exKonsumsi.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                group = groupPosition;
                child = childPosition;


                String namaMakanan = listDataChildDaftarAwal.get(
                        listDataHeaderDaftarAwal.get(groupPosition)).get(
                        childPosition);

                Toast.makeText(getApplicationContext(), namaMakanan + "  " + "sudah dihapus" , Toast.LENGTH_LONG) .show();

                try {
                     String nama = URLEncoder.encode(username, "utf-8"); 
                     System.out.println(username);
                    String namatoEn = URLEncoder.encode(namaMakanan, "utf-8"); 
                    url += "?" + "&nama=" + nama + "&namatoEn=" + namatoEn ;
                    getRequest(url);
                 }
                    catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                listDataChildDaftarAwal.remove(namaMakanan);
                return true;
            }
        });

the adapter :

public class ExpandableListAdapterKonsumsi extends BaseExpandableListAdapter {
    public static int takaran = 0;
    private Context context;
    private List<String> listDataHeader; // judul header
    private HashMap<String, List<String>> listDataChild; // child data dengan
                                                            // format judul
                                                            // header, judul
                                                            // child

    public ExpandableListAdapterKonsumsi(Context context,
            List<String> listDataHeader,
            HashMap<String, List<String>> listDataChild) {
        super();
        this.context = context;
        this.listDataHeader = listDataHeader;
        this.listDataChild = listDataChild;

    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return this.listDataChild.get(this.listDataHeader.get(groupPosition))
                .get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final String childText = (String) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(
                    R.layout.konsumsi_user_expand_listchild, null);
        }
        CheckedTextView listChild = (CheckedTextView) convertView
                .findViewById(R.id.lblListItemKonsumsi);
        listChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return this.listDataChild.get(this.listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return this.listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return this.listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(
                    R.layout.daftar_akun_expand_listgroup, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

any help would be appreciated. thanks

您必须删除与listview项相关联的数据,例如删除listDataChild中的一项,然后调用adapter.notifydatasetchanged通知listview数据已更改,然后listview将刷新视图以与数据保持同步。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM