gdantimi
gdantimi

Reputation: 388

Android: how to set item checked in gridview?

Is there a way to check an element in a gridview?

I cannot find the toggle() method or setChecked(true), my gridview has an adapter that extends BaseAdapter and I want to change background color when an element is checked (not only selected).

I would do like ListView: GridView.setChoicheMode(MULTICHOICE) and then item.toggle() or item.setChecked(true) and store the state of check into the view.

Edit:

I added an empty CheckedTextView to store the check state. But is there a cleaner way to do this?

Edit2 Now I can do what I want but when i scroll down the gridview and then I scroll up the selected items aren't selected anymore.

boolean checked = layout.getCheckedItemPositions().get(position);
                if(checked){
                    check.toggle();
                    view.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                }
                else{
                    check.toggle();
                    view.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
                }

where layout is the layout of the gridview. I think I have to modify the getView method of my adapter, but this code does not works:

CheckedTextView check = (CheckedTextView) layout.findViewById(R.id.txv_grid_check);
        boolean checked = check.isChecked();
        if(checked){
            layout.setBackgroundColor(c.getResources().getColor(android.R.color.holo_green_light));
        }
        else{
            layout.setBackgroundColor(c.getResources().getColor(android.R.color.transparent));
        }

Edit 3

I think there is no way to do what I want (store the state in the CheckedTextView element) because the views are destroyed and recreated when scrolling a list or a gridview. So I needed to handle the state of the items into the adapter. I used an HashSet of int to store the position of the checked items and I made some public method for handle this list from the gridview. In the gridview activity it is possibile to get the adapter and then do myadapter.check(int position) or uncheck(int position). Then in the adapter, into the getView() method, we need to check if a position is in the list and set the appropriate background color.

Upvotes: 2

Views: 11498

Answers (2)

itsrajesh4uguys
itsrajesh4uguys

Reputation: 4638

Use this code. it works well for me.

public View getView(final int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            if(convertView==null)
                vi = inflater.inflate(R.layout.list_row, null);

             final MyDat mydat;// = new MyDat();
            if (position >= MyViewedMeItemList.size()) {
                mydat = new MyDat();

            } 
            else
            {
                mydat = MyViewedMeItemList.get(position);
            }

            mydat.title = (TextView)vi.findViewById(R.id.title); // title
            mydat.artist = (TextView)vi.findViewById(R.id.artist); // artist name
            mydat.duration = (TextView)vi.findViewById(R.id.duration); // duration
            mydat.thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
            mydat.cccheckbomx=(CheckBox)vi.findViewById(R.id.rajesh);        
            mydat.cccheckbomx.setVisibility(View.VISIBLE);


            if (mydat.myCheckStatus) {
                // Toast.makeText(MessageInboxDelete.this, "true --->" +
                // position,
                // Toast.LENGTH_SHORT).show();
                mydat.cccheckbomx.setChecked(true);
            } else {
                /*
                 * Toast.makeText(MessageInboxDelete.this, "false --->" +
                 * position, Toast.LENGTH_SHORT).show();
                 */
                mydat.cccheckbomx.setChecked(false);
            }

           mydat.cccheckbomx.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View paramView) {
                    // TODO Auto-generated method stub
                    //Toast.makeText(context, "Rajesh", Toast.LENGTH_SHORT).show();




                    int gridchild;
                    gridchild = list.getChildCount();
                    for (int j = 0; j < gridchild; j++) {
                        // DrawArea_Steptwo.gridview.getChildAt(j);

                    //  Toast.makeText(context, "Rajesh", Toast.LENGTH_SHORT).show();
                        RelativeLayout gridchildlayout = (RelativeLayout) list
                                .getChildAt(j);

                        CheckBox tempRadioToggle = (CheckBox) gridchildlayout
                                .findViewById(R.id.rajesh);

                        tempRadioToggle.setChecked(false);                    

                        MyDat tempViewholder = MyViewedMeItemList.get(j);
                        tempViewholder.myCheckStatus = false;
                    }


                    for(int j = 0; j < MyViewedMeItemList.size(); j++) {
                        MyDat tempViewholder = MyViewedMeItemList.get(j);
                        tempViewholder.myCheckStatus = false;
                    }
                    CheckBox tempRadioToggle = (CheckBox) paramView.findViewById(R.id.rajesh);      
                    tempRadioToggle.setChecked(true);

                    MyDat tempViewholder = MyViewedMeItemList.get(position);

                            if (tempViewholder.myCheckStatus == false) {
                                tempViewholder.myCheckStatus = true;
                                // tempViewholder.deleteRadioButton.setChecked(true);


                            } else {
                                tempViewholder.myCheckStatus = false;
                                // tempViewholder.deleteRadioButton.setChecked(false);

                            }


                }
            });
            HashMap<String, String> song = new HashMap<String, String>();
            song = data.get(position);

            // Setting all values in listview
            mydat.title.setText(song.get(CustomizedListView.KEY_TITLE));
            mydat.artist.setText(song.get(CustomizedListView.KEY_ARTIST));
            mydat.duration.setText(song.get(CustomizedListView.KEY_DURATION));
            imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), mydat.thumb_image);

            if (MyViewedMeItemList.size() <= position) {
                MyViewedMeItemList.add(mydat);
            }



            return vi;
        }

     public  class MyDat{

           Boolean myCheckStatus=false;
           TextView title ;
           TextView artist;
           TextView duration;
           ImageView thumb_image;
           CheckBox cccheckbomx;

       }
    }

Upvotes: 0

TNR
TNR

Reputation: 5869

I hope this answer will help you. Here in this example there is TextView in grid item layout, which you change to a CheckedTextView or a Checkbox. In the getView() check the state and set the Background Color of your Grid item.

Upvotes: 2

Related Questions