简体   繁体   English

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

[英]How to store selected items of an ExpandableListView in an ArrayList

I have a list of users inside of an ExpandableListView , for now I have 2 groups of list, now I'm trying to create an ArrayList that will add data as I click on the users, so if have 2 groups of schools and I click on a student of each one I should have 2 positions in my array, one for each group containing its respective users, my problem is, my array has 2 positions but it is not separating the students: 我有一个ExpandableListView内的用户列表,现在我有2组列表,现在我正在尝试创建一个ArrayList ,当我点击用户时会添加数据,所以如果有两组学校我点击对于每个人的学生,我应该在我的阵列中有2个位置,每个组包含其各自的用户,我的问题是,我的阵列有2个位置,但它不是分开学生:

What I want is this: 我想要的是这个:

School A: 学校A:

student1 selected student1被选中

student2 STUDENT2

student3 selected student3入选

School B: 学校B:

student4 student4

student5 selected student5入选

Resulting in this: 结果如下:

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

Here is what I tried so far: 这是我到目前为止尝试的内容:

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

An easy solution is to create a new class SelectableObject : 一个简单的解决方案是创建一个新类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; }
}

then create your ExpandableListView like this 然后像这样创建您的ExpandableListView

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

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

    ..

    childItems.add(child);
}

Then we need to make the onSelect listener function call select function 然后我们需要使onSelect监听器函数调用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();

        ..

  })

Then we can query selected items like this 然后我们可以查询这样的选定项目

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

There is one more way to go. 还有一条路可走。 Instead of creating wrapper object you can create Map<group,List<child>> selected items and add items to this list pretty much the same way, by listening this event: 通过监听此事件,您可以创建Map<group,List<child>> selected items并以相同的方式将项目添加到此列表Map<group,List<child>> selected items而不是创建包装器对象:

mExpandableList.setOnChildClickListener(new OnChildClickListener() {
  @Override public boolean onChildClick(ExpandableListView parent,
    View v,int groupPosition, int childPosition, long id) {
      //toggle selections code here 
  }

So here is important part: (and full working github repo ) 所以这里有重要的部分:(和完整的工作github回购

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...// some init part 

    final MyAdapter adapter = new MyAdapter(schools, students);
    listView.setAdapter(adapter);

    listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, 
                   View v, int groupPosition, int childPosition, long id) {
            adapter.toggleSelection(groupPosition, childPosition);
            adapter.notifyDataSetInvalidated();
            return false;
        }
    });
}

We have several options where to put such selected items map, but in my project I use it in custom adapter class. 我们有几个选项可以放置这些选定的项目映射,但在我的项目中,我在自定义适配器类中使用它。 There is no real need for custome adapter, and it is possible to put Map<G, List<C>> selectedItems; 客户端适配器并不是真正需要,可以将Map<G, List<C>> selectedItems; and related functions (toggleSelection, isSelected, getSelectedItems) in Activity, but we still need to highlight selected sells, so adapter usually is the best place to put it. 在Activity中有相关的函数(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 

在此输入图像描述

And convert result Map to List will be an easy task: 并将结果Map转换为List将是一项简单的任务:

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

or something similar. 或类似的东西。

PS. PS。 You can also play with Map<group,List<child>> .It can be pretty much any data structure you want it to be. 您还可以使用Map<group,List<child>>它可以是您想要的任何数据结构。 2 arrays or maybe just 1 single array if you don't have duplicates in your group data. 如果您的组数据中没有重复项,则可以使用2个阵列,也可以只使用1个阵列。 You can control it, limit the number of selections and so on... 你可以控制它,限制选择的数量等等......

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

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