title: How to Get the value of selected checkbox item in android?
body: <p>I am developing quiz like application (multiple choice question) that receives the question from the server and send the answer collection to server button click. For this I have used ListView that consist TextView for the question and four checkbox items for 4 choices. I have successfully written code to receive the question with choices from the server. But now I could not figure out how to get the value of particular selected checkbox items among 4 choicebox for every question?? </p>
  
  <p><em>activity_main.xml</em></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.multiple.MainActivity" 
      &gt;
      &lt;!--android:background="@drawable/back"--&gt;
  
   &lt;TextView
       android:id="@+id/textview"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="@string/Title"
       android:background="@color/white"
       android:textColor="@color/col"
       android:gravity="center"
       android:textSize="30sp"
       /&gt;
  
     &lt;ListView
         android:id="@+id/list"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_below="@+id/textview"
         android:layout_marginBottom="40sp"
         /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/finish"
          android:id="@+id/button"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          /&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><em>list.xml</em></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="com.multiple.MainActivity" 
      &gt;
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:textStyle="bold"
          android:text="@string/Large"
          /&gt;
          &lt;!--android:textColor="@color/white"--&gt;
      &lt;CheckBox
          android:id="@+id/checkBox1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_marginLeft="30dp"
          android:layout_marginStart="30dp"
          android:layout_below="@+id/textView1"
          android:text="@string/Checkbox"
          android:textSize="25sp"
          android:background="@color/white"
          android:textColor="@color/lightblue"
          android:focusable="false"
          /&gt;
  
      &lt;CheckBox
          android:id="@+id/checkBox2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_marginLeft="30dp"
          android:layout_marginStart="30dp"
          android:layout_below="@+id/checkBox1"       
          android:text="@string/Checkbox"
          android:textColor="@color/lightblue"
          android:textSize="25sp"
          android:focusable="false"
          /&gt;
      &lt;CheckBox
          android:id="@+id/checkBox3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_marginLeft="30dp"
          android:layout_marginStart="30dp"
          android:layout_below="@+id/checkBox2"
          android:textColor="@color/lightblue"
          android:text="@string/Checkbox"
          android:textSize="25sp"
          android:focusable="false"
          /&gt;
      &lt;CheckBox
          android:id="@+id/checkBox4"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_below="@+id/checkBox3"
          android:layout_marginLeft="30dp"
          android:layout_marginStart="30dp"
          android:text="@string/Checkbox"
          android:textColor="@color/lightblue"
          android:textSize="25sp"
          android:focusable="false"
          /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><em>MainActivity.java</em></p>
  
  <pre><code>package com.multiple;
  
  import android.app.*;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.*;
  import android.widget.AdapterView.*;
  
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.util.EntityUtils;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.concurrent.ExecutionException;
  
  
  public class MainActivity extends Activity
  {
  
      private ListView listview;
      private Button finishbtn;
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          List&lt;HashMap&lt;String, String&gt;&gt; collect = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
          listview = (ListView) findViewById(R.id.list);
          finishbtn= (Button)findViewById(R.id.button);
  
          populate p = new populate();
  
          try {
              collect = p.execute().get();
          } catch (InterruptedException e) {
              e.printStackTrace();
          } catch (ExecutionException e) {
              e.printStackTrace();
          }
  
      String[] str = new String[]{"first", "second", "third", "fourth", "fifth"};
          int[] val = new int[]{R.id.textView1, R.id.checkBox1, R.id.checkBox2, R.id.checkBox3, R.id.checkBox4};
          SimpleAdapter adapter = new SimpleAdapter(this, collect, R.layout.list, str, val);
          listview.setAdapter(adapter);
  
      listview.setOnClickListener(new ListView.OnClickListener()
      {
  
          @Override
          public void onClick(View v) {
  
          }
      });
  
  }
  
      public class populate extends AsyncTask&lt; String, Void,List&lt;HashMap&lt;String,String&gt;&gt; &gt;
          {
              public List&lt;HashMap&lt;String,String&gt;&gt;  doInBackground(String... urls)
              {
                  List&lt;HashMap&lt;String,String&gt;&gt; collect= new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
                      try
                      {
  
  
                          HttpClient client = new DefaultHttpClient();
                          HttpGet post = new HttpGet("http://192.168.10.116/file.json");
                          HttpResponse res= client.execute(post);
                          HttpEntity entity = res.getEntity();
  
                          String response = EntityUtils.toString(entity);
  
  
  
                          JSONObject obj = new JSONObject(response);
                          JSONArray jsonArray = obj.optJSONArray("multiple");
  
                              Log.i("size of the array",String.valueOf(jsonArray.length()));
  
                         ArrayList&lt;JSONObject&gt;  array = new ArrayList&lt;JSONObject&gt;();
  
                          for(int i=0; i &lt; jsonArray.length(); i++)
                          {
                              JSONObject jsonObject = jsonArray.getJSONObject(i);
                              array.add(jsonObject);
  
                          }
  
                          for(int i=0;i&lt;array.size();i++){
                              JSONObject jsonObject = array.get(i);
                              String question = jsonObject.optString("question").toString();
                              String c1 = jsonObject.optString("choice1").toString();
                              String c2 = jsonObject.optString("choice2").toString();
                              String c3 = jsonObject.optString("choice3").toString();
                              String c4 = jsonObject.optString("choice4").toString();
  
  //                            Log.i("asdfas",question);
  //                            Log.i("second",c1);
  //                            Log.i("third",c2);
  //                            Log.i("fourth",c3);
  //                            Log.i("fifth",c4);
  
                              HashMap&lt;String,String&gt;  map = new HashMap&lt;String, String&gt;();
  
                              map.put("first",question);
                              map.put("second",c1);
                              map.put("third",c2);
                              map.put("fourth",c3);
                              map.put("fifth",c4);
  
                              collect.add(map);
                          }
  
  
      }
  
                      catch(IOException ex){}
  
                      catch(JSONException ex){}
  
                  return  collect;
              }
  
  
          }
  }
  </code></pre>
  
  <p>For simplicity I have also stored the image of my app
  <a href="http://i.stack.imgur.com/RSdnY.png" rel="nofollow">Application layout</a></p>