在ExpandableListview和Gridview

时间:2015-06-24 14:52:29

标签: java android gridview hashmap expandablelistview

我有一个包含2个组的ExpandableListview,并且每个组都使用gridview显示一些数据,我有一个事件突出显示孩子,当我点击它时,我想要做的是获取id值将它们存储在一个arraylist中,所以我在数组中添加了A组的ID,好吧,但是当我点击B组的子节点时,值被替换,我怎样才能获得所选项的所有id值?

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

0 个答案:

没有答案