如何在android中获取选中复选框项的值?
我正在开发类似测验的应用程序(选择题),它从服务器接收问题并将答案集发送到服务器。为此,我使用了ListView,它包括问题的TextView和4个选项的4个复选框项。我已经成功地编写了从服务器接收带有选项的问题的代码。但是现在我不知道如何在每个问题的4个choicebox中获得特定选中复选框项目的值 activity_main.xml如何在android中获取选中复选框项的值?,android,json,android-asynctask,Android,Json,Android Asynctask,我正在开发类似测验的应用程序(选择题),它从服务器接收问题并将答案集发送到服务器。为此,我使用了ListView,它包括问题的TextView和4个选项的4个复选框项。我已经成功地编写了从服务器接收带有选项的问题的代码。但是现在我不知道如何在每个问题的4个choicebox中获得特定选中复选框项目的值 activity_main.xml list.xml MainActivity.java 包com.multiple; 导入android.app.*; 导入android.os.Asyn
list.xml
MainActivity.java
包com.multiple;
导入android.app.*;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.*;
导入android.widget.AdapterView.*;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.util.EntityUtils;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.concurrent.ExecutionException;
公共类MainActivity扩展了活动
{
私有列表视图列表视图;
专用按钮完成BTN;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List collect=new ArrayList();
listview=(listview)findViewById(R.id.list);
finishbtn=(按钮)findViewById(R.id.Button);
填充p=新的填充();
试一试{
collect=p.execute().get();
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
字符串[]str=新字符串[]{“第一”、“第二”、“第三”、“第四”、“第五”};
int[]val=new int[]{R.id.textView1,R.id.checkBox1,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4};
simpledapter adapter=newsimpledapter(this,collect,R.layout.list,str,val);
setAdapter(适配器);
listview.setOnClickListener(新的listview.OnClickListener()
{
@凌驾
公共void onClick(视图v){
}
});
}
公共类任务
{
公共列表doInBackground(字符串…URL)
{
List collect=new ArrayList();
尝试
{
HttpClient=new DefaultHttpClient();
HttpGet post=新的HttpGet(“http://192.168.10.116/file.json");
HttpResponse res=client.execute(post);
HttpEntity=res.getEntity();
字符串响应=EntityUtils.toString(实体);
JSONObject obj=新的JSONObject(响应);
JSONArray JSONArray=obj.optJSONArray(“多个”);
Log.i(“数组的大小”,String.valueOf(jsonArray.length());
ArrayList数组=新的ArrayList();
for(int i=0;i 对于(int i=0;iListview不支持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()
}
});
listview.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id)
{
复选框checkbox1=(复选框)view.findViewByid(R.id.checkbox1);
复选框checkbox2=(复选框)view.findViewByid(R.id.checkbox2);
复选框checkbox3=(复选框)view.findViewByid(R.id.checkbox3);
复选框checkbox4=(复选框)view.findViewByid(R.id.checkbox4);
如果(复选框1.isChecked())
选中字符串=checkbox1.getText().tostring()
if(checkbox2.isChecked())
选中字符串=checkbox2.getText().tostring()
if(checkbox3.isChecked())
选中字符串=checkbox3.getText().tostring()
如果(复选框4.isChecked())
选中字符串=checkbox4.getText().tostring()
}
});
listView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
//TODO自动生成的方法存根
str=“”;
str=(simpledapter.adp_str).get(arg2.toString();
}
在适配器类中
将str[]声明为静态str[]
});首先查找isChecked()是否选中了复选框,然后获取字符串中的值
String answer;
if(mycheckbox.isChecked())
{
answer=mycheckbox.getText().toString();
}
如果您的问题只支持一个答案,您可以使用RadioButton
来实现此目的。关于RadioButton的链接
我想当您按下“完成”按钮时,您将收集答案。
1) 您需要遍历listview中的相对布局,然后遍历相对布局中的复选框,并使用与每个复选框对应的isChecked()条件存储值,并获取相应的值
您应该在finish按钮onclick listener中实现这一点
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
}
}
}
}
int count=listview.getAdapter().getCount();
对于(int i=0;复选框checkbox1=(CheckBox)view.findViewByid(R.id.checkbox1),当我单击包含所有选项和问题b的列表时,会引发可能重复的空指针异常
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()
}
});
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();
}
String answer;
if(mycheckbox.isChecked())
{
answer=mycheckbox.getText().toString();
}
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
}
}
}
}