Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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;
    }
});
like image 388
AND4011002849 Avatar asked Jun 08 '15 17:06

AND4011002849


People also ask

How do you store string data in a list?

To do this we use the split() method in string. The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string.

Can I store array in list?

List interface. An array can be converted to an ArrayList using the following methods: Using ArrayList. add() method to manually add the array elements in the ArrayList: This method involves creating a new ArrayList and adding all of the elements of the given array to the newly created ArrayList using add() method.

Can we store array in list in Java?

ArrayList of arrays can be created just like any other objects using ArrayList constructor.


1 Answers

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;
}
like image 197
Khaled.K Avatar answered Oct 03 '22 00:10

Khaled.K