如何在点击后从expandablelistview中的列表中删除子项?

时间:2014-06-01 10:55:45

标签: java android expandablelistview

所以我有一个ExpandableListView。单击子项后,它将从mysql表中删除数据。从表中删除数据后,我希望将其从列表中删除。

我尝试使用remove()但仍然无法正常工作。这是我的代码

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;
            }
        });

适配器:

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

}

任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

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