有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从可扩展列表视图中删除子级

我需要的是,每当点击一个孩子时,我都需要一个提醒,询问这个人是否确定要删除提醒。如果他们按yes,它需要被移除,这是怎么做到的?我的代码如下。到目前为止,我尝试过的解决方案在我尝试删除它时没有任何效果。例如,使用listDataChild.remove(childPosition)不起作用

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

import 安卓x.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;
    }
}

共 (2) 个答案

  1. # 1 楼答案

    首先使用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;
            }
        });
    
  2. # 2 楼答案

    我可以通过点击进入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;
                }
            });