title: Find the selected check boxes in a list view Android
body: <p>I am working on a project and in that I want to get the track of items that are selected (check box) from a list view.
  Is there any way of doing it?
  Code so far is below;</p>
  
  <p><strong>Main Activity</strong></p>
  
  <pre><code>import android.os.Bundle;
  import android.app.Activity;
  import android.view.Menu;
  import android.widget.Button;
  import android.widget.CheckBox;
  import android.widget.ListView;
  public class MainActivity extends Activity {
      ListView lv;
      Model[] modelItems;
      CheckBox ONE,TWO,THREE;
      Button MultiData;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          lv = (ListView) findViewById(R.id.listView1);
  
          modelItems = new Model[5];
          modelItems[0] = new Model("pizza", 0);
          modelItems[1] = new Model("burger", 0);
          modelItems[2] = new Model("olives", 0);
          modelItems[3] = new Model("orange", 0);
          modelItems[4] = new Model("tomato", 0);
          CustomAdapter adapter = new CustomAdapter(this, modelItems);
          lv.setAdapter(adapter);
  
      }
  
  }
  </code></pre>
  
  <p><strong>CustomAdapter</strong></p>
  
  <pre><code>import android.app.Activity;
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.CheckBox;
  import android.widget.TextView;
  
  import java.util.ArrayList;
  
  public class CustomAdapter extends ArrayAdapter {
      Model[] modelItems = null;
      Context context;
  
      public CustomAdapter(Context context, Model[] resource) {
          super(context, R.layout.row1, resource);
          this.context = context;
          this.modelItems = resource;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          LayoutInflater inflater = ((Activity) context).getLayoutInflater();
          convertView = inflater.inflate(R.layout.row1, parent, false);
          TextView name = (TextView) convertView.findViewById(R.id.textView1);
          CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
          name.setText(modelItems[position].getName());
          if (modelItems[position].getValue() == 1)
              cb.setChecked(true);
          else
              cb.setChecked(false);
          return convertView;
      }
  }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>public class Model{
      String name;
      int value; /* 0 -&amp;gt; checkbox disable, 1 -&amp;gt; checkbox enable */
  
      Model(String name, int value){
          this.name = name;
          this.value = value;
      }
      public String getName(){
          return this.name;
      }
      public int getValue(){
          return this.value;
      }
  
  }
  </code></pre>
  
  <p><strong>main_xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
      &lt;ListView
          android:id="@+id/listView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" &gt;
      &lt;/ListView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>row1.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" &gt;
      &lt;CheckBox
          android:id="@+id/checkBox1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="" /&gt;
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="TextView" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Eidt1 in Main Activity</strong></p>
  
  <pre><code>boolean modelItemBool[] = new boolean[modelItems.length];
          for (int h =0;h&lt;modelItemBool.length;h++){
              modelItemBool[h] = false;
          }
  </code></pre>
  
  <p><strong>Eidt 2</strong></p>
  
  <pre><code>@Override
      public View getView(final int position, View convertView, ViewGroup parent) {
          LayoutInflater inflater = ((Activity) context).getLayoutInflater();
          convertView = inflater.inflate(R.layout.row1, parent, false);
          TextView name = (TextView) convertView.findViewById(R.id.textView1);
          CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
          name.setText(modelItems[position].getName());
  
          cb.setOnCheckedChangeListener(new
            CompoundButton.OnCheckedChangeListener()  {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        modelItems[position].getValue() == 1;
                    }else{
                        modelItems[position].getValue() == 0;
                    }
                }
            });
          return convertView;
      }
  </code></pre>