title: Return checked state of checkbox in android
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2406937/android-save-checkbox-state-in-listview-with-cursor-adapter">Android save Checkbox State in ListView with Cursor Adapter</a>  </p>
  </blockquote>
  
  
  
  <p>I have a <code>ListView</code>, where each item contains some <code>text(name,id)</code> and a checkbox.There is a button to get back from <code>ListView</code> page. So a user check some items and go back to previous page. There is also a button is this page to get back to <code>ListView</code> page. When he/she gets back, I want to show him/her which items he/she had selected earlier. </p>
  
  <p>I know about <code>SharedPreferences</code>, but I don't want to use it as I don't want to save that state when user restarts the application. I only want it during application runtime. I do some job on it, save data which he has selected and also check it when he gets back. But I can't able to check the checkboxes. I hope someone would say what wrong I am doing here. I use checkbox from xml,not programmatically.</p>
  
  <pre><code>public class MyCustomArrayAdapter extends ArrayAdapter&lt;Model&gt; {
  
      private final List&lt;Model&gt; list;
  
      private final  Activity context;
  
      public static ArrayList&lt;String&gt; friendName=new ArrayList&lt;String&gt;();
      public static ArrayList&lt;String&gt; friendBirthdate=new ArrayList&lt;String&gt;();
      public static ArrayList&lt;String&gt; friendId=new ArrayList&lt;String&gt;();
      public static ArrayList&lt;String&gt; getSelectedFriendId;
      public MyCustomArrayAdapter(Activity context, List&lt;Model&gt; list) 
      {
          super(context, R.layout.list_layout, list);
  
          this.context = context;
          this.list =  list ;
      }
  
      public static int count=0;
      static class ViewHolder 
      {
          protected TextView text, sub;
          protected CheckBox checkbox;
      }
      @Override
      public View getView(int position, View convertView, ViewGroup parent) 
      {
          View view = null;
  
          if (convertView ==null)
          {
              LayoutInflater inflator = context.getLayoutInflater();
  
              view = inflator.inflate(R.layout.list_layout, null);
  
              final ViewHolder viewHolder = new ViewHolder();
  
              viewHolder.text = (TextView)view.findViewById(R.id.label);
  
              viewHolder.text.setTextColor(Color.WHITE);
  
              viewHolder.sub = (TextView)view.findViewById(R.id.sub);
  
              viewHolder.sub.setTextColor(Color.GRAY);
  
              viewHolder.checkbox = (CheckBox)view.findViewById(R.id.check);
  
              if(SelectionPage.get_selectedFriendId()!=null)   //checking whether user checked something earlier
              {
                  getSelectedFriendId=SelectionPage.get_selectedFriendId();   //get checked userid from when user selects something
  
                  for( int x = 0 ; x &lt; getSelectedFriendId.size() ; x++ )
                  {
                      for(int a=0;a&lt;list.size();a++)
                      {
                          if(list.get(a).getId().contains(getSelectedFriendId.get(x)))
                          {
                              Toast.makeText( context, "matched..", Toast.LENGTH_SHORT).show();
                              viewHolder.checkbox.setChecked(true); //this doesn't check checkbox which is the problem!
                          }
                      }
                  }
              }
              viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
              {
                  public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
                  {
                      Model element = (Model)viewHolder.checkbox.getTag();
  
                      if(buttonView.isChecked())
                      {
                          count++;
  
                          friendName.add(element.getName().toString());
                          friendBirthdate.add(element.getPlace().toString());
                          friendId.add(element.getId().toString());
                      }
                      else
                      {
                          friendName.remove(element.getName().toString());
                          friendBirthdate.remove(element.getPlace().toString());
                          friendId.remove(element.getId().toString());
                          count--;
                      }
                  }
              });
  
              view.setTag(viewHolder);
              viewHolder.checkbox.setTag(list.get(position));
  
          } 
          else 
          {
              view = convertView;
              ((ViewHolder)view.getTag()).checkbox.setTag(list.get(position));
          }
  
          ViewHolder holder = (ViewHolder) view.getTag();
          holder.text.setText(list.get(position).getName());
          holder.sub.setText( list.get(position).getPlace());
          return view;
      }
  }
  </code></pre>