2015-10-13 70 views
0

我正在开发从服务器收到问题并将答案集发送到服务器按钮单击的应用程序(选择题)的测验。为此,我使用了包含TextView的ListView和4个复选框。我已成功编写代码以接收来自服务器的选择问题。但是现在我无法弄清楚如何在每个问题的4个选择框中获得特定的复选框项目的值?如何获取android中选中的复选框项的值?

activity_main.xml中

<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" 
    > 
    <!--android:background="@drawable/back"--> 

<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" 
    /> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/textview" 
     android:layout_marginBottom="40sp" 
     /> 

    <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" 
     /> 


</RelativeLayout> 

list.xml

<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" 
    > 
    <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" 
     /> 
     <!--android:textColor="@color/white"--> 
    <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" 
     /> 

    <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" 
     /> 
    <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" 
     /> 
    <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" 
     /> 
</RelativeLayout> 

MainActivity.java

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<HashMap<String, String>> collect = new ArrayList<HashMap<String, String>>(); 
     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< String, Void,List<HashMap<String,String>> > 
     { 
      public List<HashMap<String,String>> doInBackground(String... urls) 
      { 
       List<HashMap<String,String>> collect= new ArrayList<HashMap<String, String>>(); 
        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<JSONObject> array = new ArrayList<JSONObject>(); 

         for(int i=0; i < jsonArray.length(); i++) 
         { 
          JSONObject jsonObject = jsonArray.getJSONObject(i); 
          array.add(jsonObject); 

         } 

         for(int i=0;i<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<String,String> map = new HashMap<String, String>(); 

          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; 
      } 


     } 
} 

对于SIMPLIC我也存储了我的应用程序的图像 Application layout

+1

的可能的复制[机器人:获取选中的复选框的值](http://stackoverflow.com/questions/9411540/android-get-checked-checkbox-values) – jlopez

回答

0

Listview不支持onclicklistener。使用onitemclicklistener如下

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
{ 
CheckBox checkbox1 = (CheckBox) view.findViewByid(R.id.checkbox1); 
CheckBox checkbox2 = (CheckBox) view.findViewByid(R.id.checkbox2); 
CheckBox checkbox3 = (CheckBox) view.findViewByid(R.id.checkbox3); 
CheckBox checkbox4 = (CheckBox) view.findViewByid(R.id.checkbox4); 
if(checkbox1 .isChecked()) 
     String checked = checkbox1.getText().tostring() 
if(checkbox2 .isChecked()) 
     String checked = checkbox2.getText().tostring() 
if(checkbox3 .isChecked()) 
     String checked = checkbox3.getText().tostring() 
if(checkbox4 .isChecked()) 
     String checked = checkbox4.getText().tostring() 
      } 
     }); 
+0

空指针抛出异常为复选框checkbox1 = (CheckBox)view.findViewByid(R.id.checkbox1); –

+0

上面的作品,当我点击列表,其中包括所有的选择和问题,但不是单独的列表视图中的复选框 –

+0

R.id.checkbox1是你已经在xml文件中给出你在适配器类中膨胀的ID ..检查ids r same r不是。如果不改变它 –

0
listView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 
      str = ""; 
      str = (SimpleAdapter.adp_Str).get(arg2).toString(); 

     } 

在适配器类 DECLARE STR []以静态STR [] });

0

首先发现,如果复选框被选中或不是由isChecked(),然后获得String中的值。

String answer; 

if(mycheckbox.isChecked()) 
{ 
     answer=mycheckbox.getText().toString(); 
} 

如果您的问题仅支持一个答案,您可以使用RadioButton作为此目的。有关单选按钮

http://www.mkyong.com/android/android-radio-buttons-example/

0

链接我相信你会当你按下Finish按钮,收集的答案。 1)您需要遍历listview中的相对布局,然后遍历相关布局中的复选框,并使用与每个复选框相对应的isChecked()条件存储该值,并获取相应的值。

你应该在你完成按钮onclick监听器中实现这个。

int count = listview.getAdapter().getCount(); 
for(int i=0;i<count;i++){ 
    RelativeLayout relLayout = listview.getChildAt(i); 
    for(int i=0;i< relLayout.getChildCount();i++){ 
     View v = relLayout.getChildAt(i); 
     if(v instanceof CheckBox){ 
      CheckBox c = (CheckBox)v; 
      if(c.isChecked()){ 
       String c = c.getText().tostring(); 
       //Add value to answers array or do processing 
      } 
     } 
    } 
}