繁体   English   中英

单击后如何从expandablelistview的列表中删除子级?

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

所以我有一个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;
    }

}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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