0

What I'm trying to do is get a list of item selected in gridview.

Added a checkbox to gridview when the user selects or un-select the checkbox i need to store "true" or "false" in array whether that row is selected or not .

Adapter code is as follow :

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    pos = position;
    View MyView = convertView;
    if (convertView == null) { 
        holder = new ViewHolder();
        MyView = inflater.inflate(R.layout.hospital_row, null);
        holder.tv = (TextView)MyView.findViewById(R.id.hospital_name);
        holder.address = (TextView)MyView.findViewById(R.id.address);
        holder.selected = (CheckBox)MyView.findViewById(R.id.checkBox1);

        MyView.setTag(holder);
   } else {

       holder = (ViewHolder) MyView.getTag();
       }

    holder.address.setText(addressValues[position]);
    holder.tv.setText(procedureValues[position] );   
    holder.selected.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {                
                 Log.i("adapter","pos = "+ pos);
                 if(holder.selected.isChecked()){
                        arrlist.HOSPITALSELECTED[pos] = "true";
                    }else{
                        arrlist.HOSPITALSELECTED[pos] = "false";
                    }

                    for(String sel : arrlist.HOSPITALSELECTED){
                        Log.e("Hospital Adapter", "Selected =" + sel);
                    }
            }   
        }); 
        return MyView;      
}

class ViewHolder {
    TextView tv;
    TextView address;
    CheckBox selected;
    int id;
}

Log cat : i have 3 elements in grid view , but it returns only 2 & 0 position

02-03 14:46:04.605: I/adapter(816): pos = 2

02-03 14:46:04.605: E/Hospital Adapter(816): Selected =true

02-03 14:46:04.617: E/Hospital Adapter(816): Selected =false

02-03 14:46:04.625: E/Hospital Adapter(816): Selected =true

02-03 14:46:08.015: I/adapter(816): pos = 2

02-03 14:46:08.015: E/Hospital Adapter(816): Selected =true
02-03 14:46:08.025: E/Hospital Adapter(816): Selected =false
02-03 14:46:08.025: E/Hospital Adapter(816): Selected =true```
Uday
  • 1,619
  • 3
  • 23
  • 48
  • Your views are recreating, handle that? You might want to implement a Multi-dimensional Boolean array to keep a check on positions. http://android.amberfog.com/?p=296 – Skynet Feb 03 '14 at 09:37
  • Not Multi-dimensional array , just i want to store the status on that whether it is selected or not . – Uday Feb 03 '14 at 09:43
  • I'm not getting a proper position here.if i click on first element it returns 2 some time and some time it works fine. – Uday Feb 03 '14 at 09:44
  • Yes mate, save the position in a multidimensional array. That is the only way you will get correct values back. Tried and tested! – Skynet Feb 03 '14 at 09:58
  • Another way would be, to store a bool value in the DB. For each of your entry in the Grid. – Skynet Feb 03 '14 at 10:04
  • Can you send me a snippet of example code. – Uday Feb 03 '14 at 10:04
  • DB is again complicated , i can do multi -dimensional array , can you send me a snippet of example code for that. – Uday Feb 03 '14 at 10:05
  • Check here: http://stackoverflow.com/questions/12812281/fetching-checkbox-state-in-a-gridview-item-for-all-checkbox-in-gridview-on-butto – Skynet Feb 03 '14 at 10:06
  • You welcome, kip coding :) – Skynet Feb 03 '14 at 10:42

0 Answers0