如何从可扩展列表视图中删除 child

How to remove child from Expandable Listview

我几乎需要的是,每当单击 child 时,我都需要出现一个警报,询问此人是否确定要删除提醒。如果他们按是,则需要将其删除,那是怎么做到的?我的代码如下。到目前为止,我尝试过的解决方案在尝试删除它时没有任何反应。例如,使用 listDataChild.remove(childPosition) 不起作用。

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import Recycler.ExpandableListAdapter;

public class MainActivity extends Activity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>(); 

        Button home = findViewById(R.id.HomeBtn);
        Button menu = findViewById(R.id.MenuBtn);


        home.setBackgroundResource(R.drawable.home_button);
        home.setBackgroundResource(R.drawable.btn_bg_yes_select);

        menu.setBackgroundResource(R.drawable.menu_button);
        menu.setBackgroundResource(R.drawable.btn_bg_not_select);

        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                home.setBackgroundResource(R.drawable.btn_bg_yes_select);
                menu.setBackgroundResource(R.drawable.btn_bg_not_select);
            }
        });
        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent MainToMenu = new Intent(MainActivity.this, MenuActivity.class);
                MainActivity.this.startActivity(MainToMenu);
                overridePendingTransition(0, 0);


            }
        });


        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);


        //Expand Listview on Start
        expListView.expandGroup(0);
        expListView.expandGroup(1);
        expListView.expandGroup(2);




        // Listview on child click listener
        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {


                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                alertDialog.setTitle("Remove Reminder");
                alertDialog.setMessage("Are you sure you want to delete this reminder");
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

           //Need it Here!
                        

                    }
                });
                alertDialog.show();


                return false;
            }
        });
    }


    private void prepareListData() {


        // Adding child data
        listDataHeader.add("Today");
        listDataHeader.add("Yesterday");
        listDataHeader.add("Older");

        // Adding child data
        List<String> TodayList = new ArrayList<String>();
        TodayList.add("Today Example");
        TodayList.add("Today Example");


        List<String> YesterdayList = new ArrayList<String>();
        YesterdayList.add("Yesterday Example");
        YesterdayList.add("Yesterday Example");

        List<String> OlderList = new ArrayList<String>();
        OlderList.add("Old Example");
        OlderList.add("Old Example");


        listDataChild.put(listDataHeader.get(0), TodayList); // Header, Child data
        listDataChild.put(listDataHeader.get(1), YesterdayList);
        listDataChild.put(listDataHeader.get(2), OlderList);
    }
}
public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

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

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.test_list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.test_list_group, null);
        }

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

        return convertView;
    }

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

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

我可以通过单击进入 private void prepareListData() 来修复它。然后我添加了使用子位置和组位置的代码,它得到了要从列表数据中删除的文本。

代码-

expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {


                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                alertDialog.setTitle("Remove Reminder");
                alertDialog.setMessage("Are you sure you want to delete this reminder");
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        
                        String ValueToRemove = listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition);

                        TodayList.remove(ValueToRemove);
                        YesterdayList.remove(ValueToRemove);
                        OlderList.remove(ValueToRemove);

                        listAdapter.notifyDataSetChanged();


                    }
                });
                alertDialog.show();


                return false;
            }
        });

首先用groupPosition获取选中的组项列表,然后用childPosition从组列表中删除选中的项。

expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {


            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
            alertDialog.setTitle("Remove Reminder");
            alertDialog.setMessage("Are you sure you want to delete this reminder");
            alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    List<String> selectedGroupList = listDataChild.get(listDataHeader.get(groupPosition));
                    selectedGroupList.remove(childPosition);

                    listAdapter.notifyDataSetChanged();
                }
            });
            alertDialog.show();


            return false;
        }
    });