Get all children selected inside ExpandableListview and Gridview

I have an ExpandableListview with two groups and some data in each that is displayed using a gridview. I have an event that highlights a child when I click on it, what I am trying to do is get the id of them and store them in an arraylist, so I add the group A ids in the array, ok, but when I click on the child element of group B, the value is replaced, how can I get all the ids of the selected elements

private Context context;
    public static ArrayList<CadastraEscolas> groups;
    private ArrayList<ArrayList<Filhos>> child = new ArrayList();
    private HashMap<String, GPSEscolas> aMap = new HashMap<String, GPSEscolas>();
    public static ArrayList<Filhos> listchild;
    private GridAdapter adapter;
    private View ex;
    private SecurePreferences Sessao;
    public static CustomGridView gridView;


    public ExpandListTest(Context context, ArrayList<CadastraEscolas> groups, HashMap<String, GPSEscolas> data, SecurePreferences mSessao) {
        this.context = context;
        this.groups = groups;
        this.aMap = data;
        this.Sessao = mSessao;
        child = new ArrayList();
        for (int i = 0; i < groups.size(); i++) {
            child.add(i, groups.get(i).getalunos());
        }
    }


    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {


        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.gridview, null);
        }


        listchild = new ArrayList<Filhos>();

        for (int j = 0; j < groups.get(groupPosition).getalunos().size(); j++) {

            listchild.add(child.get(groupPosition).get(j));

        }


        gridView = (CustomGridView) convertView.findViewById(R.id.GridView_toolbar);

        gridView.setExpanded(true);
        adapter = new GridAdapter(context, groups, listchild, groupPosition, aMap, Sessao);
        gridView.setAdapter(adapter);// Adapter
        gridView.setChoiceMode(CustomGridView.CHOICE_MODE_MULTIPLE);



        ex = MainFragment.rootView;



        return convertView;

    }


    @Override
    public int getChildrenCount(int nGroup) {
        return 1;

    }


    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        CadastraEscolas group = (CadastraEscolas) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_item, null);
        }
        ExpandableListView mExpandableListView = (ExpandableListView) parent;
        mExpandableListView.expandGroup(groupPosition);
        TextView tv = (TextView) convertView.findViewById(R.id.group_name);
        tv.setText(group.getNome_fantasia());

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }



}

      

GridAdapter:

 public GridAdapter(Context context, ArrayList<CadastraEscolas> groups, ArrayList<Filhos> childValues, int groupPosition, HashMap<String, GPSEscolas> hash, SecurePreferences sessao) {
        mContext = context;
        child = childValues;
        escola = groups;
        posicaoEscola = groupPosition;
        this.aMap = hash;
        this.Sessao = sessao;


    }


    @Override
    public int getCount() {
        return child.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    holder = null;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_item, null);
        holder = new ViewHolder();

        holder.text = (TextView) convertView.findViewById(R.id.name);
        convertView.setTag(holder);
        final View finalConvertView = convertView;
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isChecked = child.get(position).isChecked();
                if (!isChecked) {
                    child.get(position).setChecked(true);


                    String idsAlunos = "";
                    for (Iterator<Map.Entry<String, GPSEscolas>> it = aMap.entrySet().iterator(); it.hasNext(); ) {
                        Map.Entry<String, GPSEscolas> entry = it.next();

                        String id_escola = entry.getKey();
                        String ids_aluno = entry.getValue().getAlunos();

                        idsAlunos += ";" + ids_aluno;

                        checked.add(idsAlunos.substring(1));
                    }
                } else {
                    child.get(position).setChecked(false);                       
                }



            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.text.setText(child.get(position).getNome());
            return convertView;
        }
    }
}
static class ViewHolder {
    TextView text;
}

      

+3


source to share





All Articles