15

I'm using a GridView inside an ExpandableListView

I have a function that highlights an item when it is clicked, now I'm trying to implement a button that when pressed it will unselect all selected items, but it is unselecting only the last view clicked

public class GridAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Filhos> child;
    public ArrayList<CadastraEscolas> escola;
    private ArrayList<ArrayList<Filhos>> filhos = new ArrayList();



    public GridAdapter(Context context, ArrayList<CadastraEscolas> groups, ArrayList<Filhos> childValues, int groupPosition) {
        mContext = context;
        child = childValues;
        escola = groups;
        posicaoEscola = groupPosition;


    }


    @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, final 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();


            final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca);
            final TextView nomeAluno = (TextView) convertView.findViewById(R.id.name);



            convertView.setTag(holder);

            final View finalConvertView = convertView;
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {





                    if (list.size() > 0) {

                        isChecked = filhos.get(posicaoEscola).get(position).isChecked();


                        if (!isChecked) {

                            selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE);


                        } else {

                            selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE);

                        }



                    ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(final View v) {


                            limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE);



                        }
                    });


                }
            });


        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(child.get(position).getNome());


        return convertView;


    }


    static class ViewHolder {
        TextView text;
    }



   public void selecao(boolean check, int position, TextView nomeAluno, int color, View v, int visibility) {
    filhos.get(posicaoEscola).get(position).setChecked(check);
    nomeAluno.setTextColor(color);
    v.findViewById(R.id.overlay).setVisibility(visibility);
    v.findViewById(R.id.overlayText).setVisibility(visibility);
}

public void limpaSelecao(boolean check, int position, TextView nomeAluno, int color, ViewGroup v, int visibility) {



    for (int x = 0; x < group.size(); x++) {

        for (int j = 0; j < group.get(x).getalunos().size(); j++) {


            if(filhos.get(x).get(j).isChecked()){
                v.getChildAt(j).findViewById(R.id.loadingPanel).findViewById(R.id.overlay).setVisibility(View.GONE);
            }



            nomeAluno.setTextColor(color);
        }
    }


}

}

Layout:

<RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@drawable/nomealuno_main"
                android:layout_below="@+id/child">

                <View
                    android:id="@+id/overlay"
                    android:layout_width="125dp"
                    android:layout_height="50dp"
                    android:background="@color/bgOverlay"
                    android:visibility="gone"/>
 </RelativeLayout>

here is what is happening:

when I select all the items from A to B like that:

enter image description here

And hit clear, it will only remove the selected children from the last group that I clicked:

enter image description here

I need to remove them all when I click on Clear, all the children no matter in what group they are have to be removed.

ExpandableAdapter:

public class ExpandListTest extends BaseExpandableListAdapter {


    public static final int CHOICE_MODE_MULTIPLE = AbsListView.CHOICE_MODE_MULTIPLE;


    public static final int CHOICE_MODE_MULTIPLE_MODAL = AbsListView.CHOICE_MODE_MULTIPLE_MODAL;

    /**
     * No child could be selected
     */
    public static final int CHOICE_MODE_NONE = AbsListView.CHOICE_MODE_NONE;

    /**
     * One single choice per group
     */
    public static final int CHOICE_MODE_SINGLE_PER_GROUP = AbsListView.CHOICE_MODE_SINGLE;

    /**
     * One single choice for all the groups
     */
    public static final int CHOICE_MODE_SINGLE_ABSOLUTE = 10001;

    private Context context;
    public static ArrayList<CadastraEscolas> groups;
    private ArrayList<ArrayList<Filhos>> child = new ArrayList();
    private HashMap<String, GPSEscolas> aMap = new HashMap<String, GPSEscolas>();
    private HashMap<String, GPSEscolas> HashTask = new HashMap<String, GPSEscolas>();
    public static ArrayList<Filhos> listchild;
    private GridAdapter adapter;
    private SecurePreferences Sessao;
    public static CustomGridView gridView;
    private SparseArray<SparseBooleanArray> checkedPositions;
    private static final String LOG_TAG = ExpandListAdapter.class.getSimpleName();


    public ExpandListTest(Context context, ArrayList<CadastraEscolas> groups, HashMap<String, GPSEscolas> data, SecurePreferences mSessao, HashMap<String, GPSEscolas> hashTask) {
        this.context = context;
        this.groups = groups;
        this.aMap = data;
        this.Sessao = mSessao;
        checkedPositions = new SparseArray<SparseBooleanArray>();
        child = new ArrayList();
        if (groups != null) {
            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, final 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, child, groupPosition, aMap, HashTask, Sessao);
        gridView.setAdapter(adapter);// Adapter
        gridView.setChoiceMode(CustomGridView.CHOICE_MODE_MULTIPLE);



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

}
7
  • @pskink all right I read it, now could you help me? Jun 25, 2015 at 14:47
  • are you would like to remove a view from it parent or what ?
    – narancs
    Jun 25, 2015 at 14:52
  • @Karoly The view is gone by default, when i click on an item I set it visible, it can be multiple choice, now I want to remove that view when I click on "clean" button Jun 25, 2015 at 14:58
  • are you aware that you have two onClick methods? Jun 26, 2015 at 15:15
  • @LuisSieira yes, the second one is where the clean function will execute Jun 26, 2015 at 16:34

2 Answers 2

6

Android ViewGroups can contain any number of Views, but Views can only have one parent ViewGroup, if you try to add a View that already has a parent, you will get this exception (from ViewGroup.addViewInner()):

throw new IllegalStateException("The specified child already has a parent. " +
                "You must call removeView() on the child's parent first.");

This means that the structure of your layouts is that of a tree (not a graph), and so you can use any tree traversal algorithm to iterate through every View of your layout.

The following is one of the possible algorithms, which is a post-order traversal.

enter image description here

It goes through the root element, takes the first child and makes a recursive call for the algorithm, then its second, third etc...when there are no children left to go through, it calls your deselect function for the node.

For the tree in the diagram, nodes will be unselected in this order:

A,C,E,D,B,H,I,G, F

public void onClickTheButton(View view) {
    unselectall(R.layout.your_layout);
}

public void unselectAll(View view) {
    if(view instanceof ViewGroup) {
        for(int ii = 0 ; ii<(ViewGroup)view.getChildrenCount(); ii++) {
            unselectAll((ViewGroup)view.getChildAt(ii));
        }
    }
    unselect(view);
}

You can find many other ways to do it here: https://en.wikipedia.org/wiki/Tree_traversal

It is a must to learn how those algorithms work if you want to ease your programming experience.

5
  • I've edited the question, since I have an ExpandableListView with 2 groups inside, so I have 2 ViewGroups, how can I get all of the children at once? Jun 25, 2015 at 19:53
  • explain me how this work, so I can accept your answer as the right one Jun 25, 2015 at 22:18
  • I've updated the question, still not working, it only removes the last item clicked Jun 26, 2015 at 14:30
  • 1
    Here: convertView = inflater.inflate(R.layout.child_item, null); final View finalConvertView = convertView; This is the element you're iterating through, only it's children are deselected. I don't know if thats what you want to do. You should give an id to your main RelativeLayout and call unselectAll(your_main_view) Jun 26, 2015 at 15:13
  • I've add some images to the question, see if you can help me, I appreciate your patience Jun 26, 2015 at 16:55
1

change your getView() method as follows.

@Override
public View getView(final int position, View convertView, final 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();

        final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

   holder.text.setText(child.get(position).getNome());

   final View finalConvertView = convertView;
   convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (list.size() > 0) {
                isChecked = filhos.get(posicaoEscola).get(position).isChecked();

                if (!isChecked) {
                    selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE);
                } else {
                    selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE);
                }

                ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View v) {

                        limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE);
                    }
                });
            }
        });

        return convertView;
    }
2
  • Nope, getting the same results, only the last group clicked is unselecting Jul 1, 2015 at 11:32
  • Are you sure you are passing a reference to the view that contains both sections? Is it always the same one that fête unselected, or is it the one that contains the pupil you're clicking? Jul 4, 2015 at 6:57

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.