12

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:

What I want is this:

School A:

student1 selected

student2

student3 selected

School B:

student4

student5 selected

Resulting in this:

[0]-> student 1,3 [1] ->student 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;
    }
});
3
  • Have you tried using a custom ExpandableListAdapter? It might be easier to keep track of everything including the "onChecked" listener as well as an ArrayList object attached to the adapter. There are quite a few examples online: here's a YouTube video for the adapter youtu.be/-tAH1hW_tYc Jun 10, 2015 at 15:01
  • @c0d3blooded I'm using a custom ExpandableListAdapter Jun 10, 2015 at 16:51
  • You need to create array list of student(child) model class in GPSEscolas(parent) model class, and manage some booleans on click or selection. Please follow this javacodegeeks.com/2013/06/…. Let me know if you need further assistance.
    – Chitrang
    Jun 11, 2015 at 5:31

2 Answers 2

4

An easy solution is to create a new class 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

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

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;
}
5
  • Thank you for your help, but I'm having some trouble implementing your code, like in the getAllChildrenMap function, where is the "position" defined? the same for the method isSelected and getObject Jun 10, 2015 at 12:18
  • Also, i'm getting an error when I click on a child, java.lang.Integer cannot be cast to com.clickpegue.models.SelectableObject Jun 10, 2015 at 13:01
  • @WARpoluido fixed onClick, added getSelectedChildren
    – Khaled.K
    Jun 11, 2015 at 9:46
  • 1
    @WARpoluido fixed the code, it seems you have to use the Adapter to get the child object
    – Khaled.K
    Jun 11, 2015 at 10:42
  • @WARpoluido There are other solutions that are technically better, but I think that it will make the usage of it more complex, simplifying usage lead to higher readability, and maintainability
    – Khaled.K
    Jun 14, 2015 at 8:13
2
+50

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:

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 )

@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; 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.

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 

enter image description here

And convert result Map to List will be an easy task:

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. You can also play with Map<group,List<child>> .It can be pretty much any data structure you want it to be. 2 arrays or maybe just 1 single array if you don't have duplicates in your group data. You can control it, limit the number of selections and so on...

1
  • do u hv any tutorial to make that list? I need to make something like that Mar 21, 2017 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.