Java 如何在ArrayList中存储ExpandableListView的选定项

Java 如何在ArrayList中存储ExpandableListView的选定项,java,android,arrays,multidimensional-array,arraylist,Java,Android,Arrays,Multidimensional Array,Arraylist,我在一个ExpandableListView中有一个用户列表,因为现在我有两组列表,现在我正在尝试创建一个ArrayList,当我点击用户时,它会添加数据,所以如果有两组学校,我点击每个学校的一个学生,我应该在我的数组中有两个位置,每个包含各自用户的组一个,我的问题是,我的数组有2个位置,但它没有分隔学生: 我想要的是: 学校A: 学生1入选 学生2 学生3入选 学校B: 学生4 学生5入选 因此: [0]->学生1,3[1]->学生5 以下是我迄今为止所做的尝试: mGpsEscolas =

我在一个
ExpandableListView
中有一个用户列表,因为现在我有两组列表,现在我正在尝试创建一个
ArrayList
,当我点击用户时,它会添加数据,所以如果有两组学校,我点击每个学校的一个学生,我应该在我的数组中有两个位置,每个包含各自用户的组一个,我的问题是,我的数组有2个位置,但它没有分隔学生:

我想要的是:

学校A:

学生1入选

学生2

学生3入选

学校B:

学生4

学生5入选

因此:

[0]->学生1,3[1]->学生5

以下是我迄今为止所做的尝试:

mGpsEscolas = new GPSEscolas();
mArrayEscolas = new ArrayList<GPSEscolas>();
aMap = new HashMap<String, GPSEscolas>();

ExpandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(final ExpandableListView parent, View v, final int groupPosition, final int childPosition, final long id) {
        ExpAdapter.setClicked(groupPosition, childPosition);

        index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
        parent.setItemChecked(index, true);
        parent.setSelectedChild(groupPosition, childPosition, true);
        parent.getChildAt(index);

        IdAlunos = String.valueOf(mMainRest.mArrayList.get(groupPosition).getalunos().get(childPosition).getId_aluno());
        IdEscola = String.valueOf(mMainRest.mArrayList.get(groupPosition).getId_escola());

        ids_alunos.add(IdAlunos);


        notificar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {






int groupCount = ExpandList.getExpandableListAdapter().getGroupCount();

                        for (int group = 1; group <= groupCount; group++) {
                            int gcount = ExpandList.getExpandableListAdapter().getChildrenCount(groupPosition);
                            mArrayEscolas = new ArrayList<GPSEscolas>();

                            for (int child = 1; child <= gcount; child++) {

                                mGpsEscolas.setIds_alunos(String.valueOf(IdAlunos).substring(1));
                                mGpsEscolas.setId_escola(Integer.valueOf(IdEscola));
                                mGpsEscolas.setLatitude(latitudeEscola);
                                mGpsEscolas.setLongitude(longitudeEscola);
                                mGpsEscolas.setDistancia(mMainRest.RaioEscola);

                                mArrayEscolas.add(mGpsEscolas);

                                if (ExpAdapter.isChildSelectable(groupPosition, childPosition)) {
                                    aMap.put(ExpandList.getExpandableListAdapter().getChildId(group, child), mArrayEscolas);
                                }


                            }
                        }


                }
        });

        return false;
    }
});
mGpsEscolas=new-GPSEscolas();
mArrayEscolas=新的ArrayList();
aMap=newhashmap();
ExpandList.setOnChildClickListener(新的ExpandableListView.OnChildClickListener(){
@凌驾
公共布尔onChildClick(最终可展开列表视图父视图、视图v、最终int-groupPosition、最终int-childPosition、最终long-id){
ExpAdapter.setClicked(groupPosition、childPosition);
index=parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition,childPosition));
parent.setItemChecked(index,true);
parent.setSelectedChild(groupPosition、childPosition、true);
parent.getChildAt(索引);
IdAlunos=String.valueOf(mMainRest.mArrayList.get(groupPosition.getalunos().get(childPosition.getId_aluno());
IdEscola=String.valueOf(mMainRest.mArrayList.get(groupPosition.getId_escola());
ID_alunos.add(IdAlunos);
notifical.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
int groupCount=ExpandList.getExpandableListAdapter().getGroupCount();

对于(int group=1;group一个简单的解决方案是创建一个新类
SelectableObject

class SelectableObject<T>
{
    boolean sel; T obj;
    public SelectableObject<T>(T obj) { this.obj=obj; this.sel=false; }
    public void select() { this.sel=true; }
    public void deselect() { this.sel=false; }
    public boolean isSelected() { return this.sel; }
    public T getObject() { return this.obj; }
}
然后我们需要使
onSelect
listener函数调用
select
函数

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

        SelectableObject<GPSEscolas> item = (SelectableObject<GPSEscolas>)
              parent.getExpandableListAdapter().getChild(groupPosition,childPosition);

        if(!item.isSelected()) item.select();
        else item.deselect();

        ..

  })
mExpandableList.setOnChildClickListener(新的OnChildClickListener(){
@重写公共布尔onChildClick(ExpandableListView父级,
视图v,int-groupPosition,int-childPosition,长id){
SelectableObject项=(SelectableObject)
parent.getExpandableListAdapter().getChild(groupPosition,childPosition);
如果(!item.isSelected())item.select();
else项。取消选择();
..
})
然后我们可以像这样查询所选项目

public void setChildData() 
{
    ArrayList<SelectableObject<GPSEscolas>> child
         = new ArrayList<SelectableObject<GPSEscolas>>();

    child.add(new SelectableObject<GPSEscolas>(new GPSEscolas(..)));

    ..

    childItems.add(child);
}
public static ArrayList<GPSEscolas> getSelectedChildren(ExpandableListView listView)
{
    ArrayList<GPSEscolas> list = new ArrayList<GPSEscolas>();

    int count = listView.getGroupCount();

    for (int group = 1; group <= count; group++)
    {
      int gcount = listView.getChildrenCount(position);

      for (int child = 1; child <= gcount; child++)
      {
          SelectableObject<GPSEscolas> item = (SelectableObject<GPSEscolas>)
            listView.getExpandableListAdapter().getChild(groupPosition,childPosition);

          // Here is where you can see the solution beauty

          if (item.isSelected())
          {
              list.add(item.getObject());
          }
      }
    }

    return list;
}
公共静态数组列表getSelectedChildren(ExpandableListView listView)
{
ArrayList=新建ArrayList();
int count=listView.getGroupCount();

对于(int group=1;group还有一条路要走。不创建包装器对象,您可以创建
Map selected items
,并以几乎相同的方式将项目添加到此列表,方法是通过侦听以下事件:

mExpandableList.setOnChildClickListener(new OnChildClickListener() {
  @Override public boolean onChildClick(ExpandableListView parent,
    View v,int groupPosition, int childPosition, long id) {
      //toggle selections code here 
  }
所以这里是重要的部分:(和充分的工作)

我们有几个选项可以将这些选定项映射到哪里,但在我的项目中,我在自定义适配器类中使用它。不需要自定义适配器,而且可以放置
map selectedItems;
和相关函数(toggleSelection、isSelected、getSelectedItems)在活动中,但我们仍然需要突出显示选定的销售,因此适配器通常是放置它的最佳位置

private class MyAdapter<G, C> extends BaseExpandableListAdapter {
    private List<G> groups;
    private Map<G, List<C>> childMap;
    private Map<G, List<C>> selectedItems;

    public MyAdapter(List<G> groups, Map<G, List<C>> childMap){
        this.groups = groups;
        this.childMap = childMap;
        this.selectedItems = new HashMap<>();
    }

    public boolean isSelected(int groupPosition, int childPosition){
        G group = groups.get(groupPosition);
        // getChild is adapter Fn and is the same as
        // G group = groups.get(groupPosition)
        // C child = childMap.get(group).get(childPosition);
        C child = getChild(groupPosition, childPosition);
        List<C> sel = selectedItems.get(group);
        return sel != null && sel.contains(child);
    }

    public void toggleSelection(int groupPosition, int childPosition){
        G group = groups.get(groupPosition);
        C child = getChild(groupPosition,childPosition);
        List<C> sel = selectedItems.get(group);
        if (sel == null){
            sel = new ArrayList<>(); // Lasy arrays creation
            //can init all arrays in constructor and never check for nulls
            selectedItems.put(group, sel);
        }
        if (sel.contains(child)) sel.remove(child);
        else sel.add(child);
    }
    ... // Adapter fns can find in git repo 
私有类MyAdapter扩展了BaseExpandableListAdapter{
私人名单组;
私有地图儿童地图;
私有地图选择系统;
公共MyAdapter(列表组、映射子映射){
这个组=组;
this.childMap=childMap;
this.selectedItems=newhashmap();
}
选择公共布尔值(int-groupPosition,int-childPosition){
G group=groups.get(groupPosition);
//getChild是适配器Fn,与
//G group=groups.get(groupPosition)
//C child=childMap.get(group.get(childPosition));
C child=getChild(groupPosition,childPosition);
List sel=selectedItems.get(组);
返回sel!=null&&sel.contains(子项);
}
公共无效切换选择(int-groupPosition、int-childPosition){
G group=groups.get(groupPosition);
C child=getChild(groupPosition,childPosition);
List sel=selectedItems.get(组);
如果(sel==null){
sel=新建ArrayList();//创建Lasy数组
//可以初始化构造函数中的所有数组,并且从不检查空值
选择editems.put(组,选择);
}
如果(选择包含(子项))选择移除(子项);
else sel.add(child);
}
…//适配器fns可以在git repo中找到

将结果映射转换为列表将是一项简单的任务:

private ArrayList<String> selectedAsList(Map<String, List<String>> selectedItems){
     ArrayList<String> result =  new ArrayList<>();
    for(List<String> students: selectedItems.values())
        result.addAll(students);
    return result;
}
private ArrayList selectedAsList(映射selectedItems){
ArrayList结果=新建ArrayList();
对于(列出学生:selectedItems.values())
结果:addAll(学生);
返回结果;
}
或者类似的东西

另外,你也可以玩
Map
。它几乎可以是任何一种
您想要的数据结构。2个数组,或者如果您的组数据中没有重复的,可能只有1个数组。您可以控制它,限制选择的数量等等…

谢谢您的帮助,但是我在实现代码时遇到了一些问题,比如在getAllChildrenMap函数中,“位置”在哪里定义?方法isSelected和GetObject也是如此,我在单击子对象时出错,java.lang.Integer不能转换为com.clickpegue.models。SelectableObject@WARpoluido修复了
onClick
,添加了
getSelectedChildren
@warpoluid要修复代码,似乎必须使用适配器来获取子对象t@WARpoluido还有其他技术上更好的解决方案,但我认为这将使it的使用更加复杂
private ArrayList<String> selectedAsList(Map<String, List<String>> selectedItems){
     ArrayList<String> result =  new ArrayList<>();
    for(List<String> students: selectedItems.values())
        result.addAll(students);
    return result;
}