所需变量
public List<String> radiobuttonSelection = null;//用来记录结果集
private Map<Integer, List<Boolean>> checkBoxMap = new HashMap<Integer, List<Boolean>>();//用来记录每个多选题中各个checkbox的选中情况
public ViewHolder holder;
public class ViewHolder {
TextView titleText;
RadioGroup choiceRG;
}
变量初始化
private void initialcheckBoxMap(int problemSize) {
for (int i = 0; i < problemSize; i++) {
if (type == 1) {//type 1->多选,0->单选
checkBoxMap.put(i, initialProblemCheckStateBoolList(choiceQuestionSize));
}
}
}
private List<Boolean> initialProblemCheckStateBoolList(int length) {// 将每个问题中选项的checkbox初始化为false
List<Boolean> checkboxList = new ArrayList<Boolean>();//checkboxList->用来记录每个问题中选项的选中情况
for (int i = 0; i < length; i++) {
checkboxList.add(false);
}
return checkboxList;
}
protected void memsetRadiobuttonSelection() {
for (int i = 0; i < lists.size(); i++)
radiobuttonSelection.add("");
}
生成radiobutton组或者checkbox组的函数
/**
* 初始化单选题的选项,并设置监听事件
*
* @param questionId
* 问卷中 问题的编号
* @param choiceId
* 问题中 选项的编号
* @param questiontitle
* 问卷中 问题题目
* @return
*/
private void initialBtGroup(final int questionId, final int choiceId,
, String questiontitle, ViewHolder holder) {
final RadioButton dotButton = new RadioButton(mContext);
dotButton.setId(choiceId);
RadioGroup.LayoutParams btnParam = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.FILL_PARENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
btnParam.leftMargin = 15;
dotButton.setLayoutParams(btnParam);
dotButton.setPadding(30, 0, 0, 0);
dotButton.setTextSize(15);
dotButton.setTextColor(R.color.darkgrey);
dotButton.setText(questiontitle);
dotButton.setButtonDrawable(R.drawable.dot_bg2);
dotButton.setTag(choiceId);// 设置当前位置
// 为点注册checked事件,当点击对应的点时,Viewpager切换到对应的page,并将点击的点设置为高亮
dotButton
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked)
// questionId->第i个问题,choiceId->第i个选项,将问题结果保存到radiobuttonSelection
radiobuttonSelection.set(questionId, Integer.toString(choiceId));
}
});
holder.choiceRG.addView(dotButton);
}
/**
* 初始化多选题的选项,并设置监听事件
*
* @param questionId
* 问卷中 问题的编号
* @param choiceId
* 问题中 选项的编号
* @param choiceLength
* 问题中 选项的总数
* @param questiontitle
* 问卷中 问题题目
* @return
*/
private void initialCheckboxGroup(final int questionId, final int choiceId,
final int choiceLength, String questiontitle,
ViewHolder holder) {
final CheckBox dotButton = new CheckBox(mContext);
dotButton.setId(choiceId);
RadioGroup.LayoutParams btnParam = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.FILL_PARENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
btnParam.leftMargin = 15;
btnParam.topMargin = 2;
dotButton.setLayoutParams(btnParam);
dotButton.setPadding(30, 0, 0, 0);
dotButton.setTextSize(15);
dotButton.setTextColor(R.color.darkgrey);
dotButton.setText(questiontitle);
dotButton.setButtonDrawable(R.drawable.dot_bg3);
dotButton.setTag(choiceId);// 设置当前位置 // 为点注册checked事件
dotButton// 此时dotbutton是一个个checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked)
checkBoxMap.get(questionId).set(choiceId, true);
else
checkBoxMap.get(questionId).set(choiceId, false);
String idSet = "";//每次check事件发生变化,就重新设置一下第i个问题的结果集
for (int i = 0, flag = 0; i < choiceLength; i++)// initialCheckboxGroup
{
if (checkBoxMap.get(questionId).get(i)) {
if (flag != 0)
idSet += "|";
idSet += Integer.toString(choiceId);
flag = 1;
}
// System.out.print(idSet);
}
radiobuttonSelection.set(questionId, idSet);// id->选项id,position->第i个问题,count->第i个选项,
}
});
holder.choiceRG.addView(dotButton);
}
这时候radiobuttonSelection中就保存了所有的结果集了。
getview函数
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.adapter_question_list_item,
null);
holder.titleText = (TextView) convertView
.findViewById(R.id.question_list_item_title);
holder.choiceRG = (RadioGroup) convertView
.findViewById(R.id.question_list_item_choiceRg);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
holder.titleText.setText(Integer.toString(position + 1) + "."
+ posts.get(position).getString("title"));
int type = posts.get(position).getInt("type");
JSONArray ar = posts.get(position).getJSONArray("xxs");
JSONObject temp = new JSONObject();
holder.choiceRG.removeAllViews();// 要先清空原有的子项
for (int i = 0; i < ar.length(); i++) {
temp = ar.getJSONObject(i);
String choiceTitle = null;
if (done == 0)
choiceTitle = temp.getString("title");
else
choiceTitle = temp.getString("title") + " 选择人数:"
+ temp.getString("num");
String id = temp.getString("id");
if (type == 0)// 如果type是0,则生成radiobutton
initialBtGroup(position, i, id, choiceTitle, holder);
else
initialCheckboxGroup(position, i, ar.length(), id,
choiceTitle, holder);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
xml中,问题item的设置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="5dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/content_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/question_list_item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:gravity="center_vertical|left"
android:padding="3dp"
android:text="会议问卷"
android:textColor="#000" />
<RadioGroup
android:id="@+id/question_list_item_choiceRg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:orientation="vertical"
android:padding="2dp"
android:text="我的调查结果"
android:textColor="#408080"
android:textSize="12dp" >
</RadioGroup>
</LinearLayout>
</LinearLayout>
效果图(请忽略选择人数部分,那是一个小项目的结果):