您好我是Android工作室的新手,我正在我的Android应用程序上创建一个测验页面。但是,我无法动态地为每个问题的无线电组添加单选按钮。有人可以指导我吗?以下是我的代码:
public class QuizActivity extends ActionBarActivity {
ArrayList<Question> questionList = new ArrayList<Question>();
ArrayList<Choice> choiceList = new ArrayList<Choice>();
List<String> listDataHeader;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
QuestionItem item;
List<QuestionItem> items;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
context = this;
listAdapter = new ExpandableListAdapter(this);
items = new ArrayList<QuestionItem>();
items = fillData(items);
listAdapter.setData(items);
//get listview
expListView = (ExpandableListView) findViewById(R.id.explist_view);
expListView.setAdapter(listAdapter);
expListView.setDivider(null);
for(int i=0; i < listAdapter.getGroupCount(); i++) {
expListView.expandGroup(i);
}
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
// Doing nothing
return true;
}
});
}
public List<QuestionItem> fillData(List<QuestionItem> itemsGroup) {
try{
TinyDB db = new TinyDB(this);
CacheObject co;
co = (CacheObject)db.getObject("cache", CacheObject.class);
questionList = co.getQuestionList();
choiceList = co.getChoiceList();
this.items = itemsGroup;
Question q;
Choice c;
for(int i=0;i<questionList.size();i++){
q = questionList.get(i);
item = new QuestionItem();
item.name = q.getQuestionContent();
item.id = q.getQuestionID();
for(int j=0;j<choiceList.size();j++){
c = choiceList.get(j);
if(c.getQuestionID()==q.getQuestionID()){
ChoiceItem child = new ChoiceItem();
child.name = c.getChoiceContent();
child.id = c.getChoiceID();
child.questionID = c.getQuestionID();
child.score = c.getChoiceScore();
item.choiceItems.add(child);
}
}
items.add(item);
}
co.setChoiceList(choiceList);
co.setQuestionList(questionList);
db.putObject("cache",co);
} catch (Exception e){
e.printStackTrace();
}
return items;
}
public class ExpandableListAdapter extends BaseExpandableListAdapter {
Context context;
private LayoutInflater inflater;
private RadioGroup radioGroup;
private List<QuestionItem> items;
public ExpandableListAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public void setData(List<QuestionItem> items) {
this.items = items;
}
@Override
public ChoiceItem getChild(int groupPosition, int childPosition) {
return items.get(groupPosition).choiceItems.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder childHolder;
ChoiceItem choice = getChild(groupPosition, childPosition);
if (convertView == null) {
childHolder = new ChildHolder();
convertView = inflater.inflate(R.layout.choice_listview_row, null);
childHolder.radio = (RadioButton) convertView.findViewById(R.id.choice);
convertView.setTag(childHolder);
} else {
childHolder = (ChildHolder) convertView.getTag();
}
childHolder.radio.setText(choice.name);
childHolder.radio.setId(choice.id);
//Log.e("ChoiceID",childHolder.radio.getId()+"");
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return items.get(groupPosition).choiceItems.size();
}
@Override
public QuestionItem getGroup(int groupPosition) {
return items.get(groupPosition);
}
@Override
public int getGroupCount() {
return items.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
GroupHolder holder;
QuestionItem item = getGroup(groupPosition);
if (convertView == null) {
holder = new GroupHolder();
convertView = inflater.inflate(R.layout.question_listview_row, null);
holder.radioGroup = (RadioGroup) convertView.findViewById(R.id.question);
holder.radioGroup.setTag(groupPosition);
holder.content = (TextView) convertView.findViewById(R.id.questionRow);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.content.setTypeface(null, Typeface.BOLD);
holder.content.setText(item.name);
convertView.setTag(holder);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
private static class QuestionItem {
String name;
int id;
List<ChoiceItem> choiceItems = new ArrayList<ChoiceItem>();
}
private static class ChoiceItem {
String name;
int id;
int questionID;
int score;
}
private static class ChildHolder {
RadioButton radio;
}
private static class GroupHolder {
TextView content;
RadioGroup radioGroup;
}
}
编辑:我已设法将按钮添加到广播组中,但现在我遇到了这个问题,其中添加了其他按钮。下面是截图和我在适配器中更改的代码:
在视图中emulator,问题1应该只有2个选项,但是当我向下和向上滚动时,其他按钮会被添加。
public class ExpandableListAdapter extends BaseExpandableListAdapter {
Context context;
private LayoutInflater inflater;
private List<QuestionItem> items;
private ArrayList<RadioGroup> rgList = new ArrayList<RadioGroup>();
public ExpandableListAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public void setData(List<QuestionItem> items) {
this.items = items;
}
@Override
public ChoiceItem getChild(int groupPosition, int childPosition) {
return items.get(groupPosition).choiceItems.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder childHolder;
ChoiceItem choice = getChild(groupPosition, childPosition);
if (convertView == null) {
childHolder = new ChildHolder();
convertView = inflater.inflate(R.layout.choice_listview_row, parent, false);
childHolder.radio = new RadioButton(context);
switch(childPosition){
case 0:
childHolder.radio.setId(R.id.choice1);
break;
case 1:
childHolder.radio.setId(R.id.choice2);
break;
case 2:
childHolder.radio.setId(R.id.choice3);
break;
case 3:
childHolder.radio.setId(R.id.choice4);
break;
case 4:
childHolder.radio.setId(R.id.choice5);
break;
case 5:
childHolder.radio.setId(R.id.choice6);
break;
case 6:
childHolder.radio.setId(R.id.choice7);
break;
case 7:
childHolder.radio.setId(R.id.choice8);
break;
case 8:
childHolder.radio.setId(R.id.choice9);
break;
case 9:
childHolder.radio.setId(R.id.choice10);
break;
}
childHolder.radio.setTag(childPosition);
if(choice.questionID == groupPosition+1) {
childHolder.radio.setText(choice.name);
rgList.get(groupPosition).addView(childHolder.radio);
Log.e("testAdd",rgList.get(groupPosition).getId()+"");
}
convertView.setTag(childHolder);
} else {
childHolder = (ChildHolder) convertView.getTag();
}
childHolder.radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.e("Hello","");
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return items.get(groupPosition).choiceItems.size();
}
@Override
public QuestionItem getGroup(int groupPosition) {
return items.get(groupPosition);
}
@Override
public int getGroupCount() {
return items.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
GroupHolder holder;
QuestionItem item = getGroup(groupPosition);
if (convertView == null) {
holder = new GroupHolder();
convertView = inflater.inflate(R.layout.question_listview_row, parent, false);
holder.content = (TextView) convertView.findViewById(R.id.questionRow);
holder.radioGroup = (RadioGroup) convertView.findViewById(R.id.question1);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
//Log.e("radioGroupID", holder.radioGroup.getTag()+"");
holder.content.setTypeface(null, Typeface.BOLD);
holder.content.setText(item.name);
switch(groupPosition){
case 0:
holder.radioGroup.setId(R.id.question1);
break;
case 1:
holder.radioGroup.setId(R.id.question2);
break;
case 2:
holder.radioGroup.setId(R.id.question3);
break;
case 3:
holder.radioGroup.setId(R.id.question4);
break;
case 4:
holder.radioGroup.setId(R.id.question5);
break;
case 5:
holder.radioGroup.setId(R.id.question6);
break;
case 6:
holder.radioGroup.setId(R.id.question7);
break;
case 7:
holder.radioGroup.setId(R.id.question8);
break;
case 8:
holder.radioGroup.setId(R.id.question9);
break;
case 9:
holder.radioGroup.setId(R.id.question10);
break;
}
holder.radioGroup.setTag(groupPosition);
if (!rgList.contains(holder.radioGroup.getTag(groupPosition))) {
rgList.add(holder.radioGroup);
}
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
private static class QuestionItem {
String name;
int id;
List<ChoiceItem> choiceItems = new ArrayList<ChoiceItem>();
}
private static class ChoiceItem {
String name;
int id;
int questionID;
int score;
}
private static class ChildHolder {
RadioButton radio;
}
private static class GroupHolder {
TextView content;
RadioGroup radioGroup;
}
}