我正在开展测验申请。它包含两种类型的测试。 第一个测试包含已修复3个选项的问题。 * 第二个测试包含未修复选项的问题。即根据问题,选项可能是4或5或6。 * 测试结束后,我需要显示评论页面。以下是第一类带有固定选项的测试的评论页面代码。(评论页面应该包含当时为测试显示的问题)< / p>
我的代码:
Review.java
public class Review extends Activity {
static ArrayList selectedoptionids = Test1.listarray;
static ArrayList<ArrayList<String>> questionslist = Test2.stringList1;
static ArrayList<ArrayList<String>> alloptionlist = Test2.optionstablelist;
static ArrayList<ArrayList<String>> all = new ArrayList<ArrayList<String>>();
ListView list;
Button next;
String op1, op2, op3, op4, op5;
ArrayList<String> arr1;
ArrayList<String> arr2;
int a, i;
static int k = 0;
static int p = 1;
static List<mainlist> entirelist = new ArrayList<mainlist>();
String quest;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.review);
list = (ListView) findViewById(R.id.listlist);
View header = getLayoutInflater().inflate(
R.layout.listview_header_text, null);
list.addHeaderView(header, null, false);
View footerView = getLayoutInflater().inflate(
R.layout.listview_footer_text, null);
list.addFooterView(footerView, null, false);
next = (Button) findViewById(R.id.next);
eachquestion();
lvAdapter adapter = new lvAdapter(this, entirelist) {
public boolean areAllItemsEnabled() {
return false;
}
public boolean isEnabled(int position) {
return false;
}
};
list.setAdapter(adapter);
}
public void eachquestion() {
arr1 = new ArrayList<String>();
for (i = p - 1; i < p + 3; i++) {
arr1 = questionslist.get(i);
arr2 = new ArrayList<String>();
for (int j = 0; j < 5; j++) {
arr2 = alloptionlist.get(0);
if (j == 0)
op1 = arr2.get(2);
else if (j == 1)
op2 = arr2.get(2);
else if (j == 2)
op3 = arr2.get(2);
}
}
entirelist.add(new mainlist(quest, op1, op2, op3));
}
}
}
mainlist.java
public class mainlist {
String question,option1,option2,option3;
public mainlist(String question,String option1,String option2,String option3) {
super();
this.question = question;
this.option1 = option1;
this.option2= option2;
this.option3 = option3;
}
public String getquestion() {
return question;
}
public void setquestion(String question) {
this.question = question;
}
public String getoption1() {
return option1;
}
public void setoption1(String option1) {
this.option1 = option1;
}
public String getoption2() {
return option2;
}
public void setoption2(String option2) {
this.option2 = option2;
}
public String getoption3() {
return option3;
}
public void setoption3(String option3) {
this.option3 = option3;
}
}
lvAdapter.java
public class lvAdapter extends BaseAdapter implements OnClickListener {
private Context context;
List<mainlist> list11 = Review.entirelist;
public lvAdapter(Context context, List<mainlist> list11 ) {
this.context = context;
this.list11 = list11 ;
}
public int getCount() {
return list11.size();
}
public Object getItem(int position) {
return list11.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
mainlist inst = list11.get(position);
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.reviewrow, null);
}
TextView question = (TextView) convertView.findViewById(R.id.textView2);
tvPhone.setText(inst.getquestion());
TextView option1 = (TextView) convertView.findViewById(R.id.op1);
option1.setText(inst.getoption1());
TextView option2 = (TextView) convertView.findViewById(R.id.op2);
option2.setText(inst.getoption2());
TextView option3 = (TextView) convertView.findViewById(R.id.op3);
option3.setText(inst.getoption3());
return convertView;
}
@Override
public void onClick(View v) {
}
}
现在我的问题是第一次测试,因为我们知道选项的数量是3所以我已经为1个问题和3个选项编写了setter和getter方法,并且工作正常。但是对于第二种类型的测试如何编写 mainlist.java类,以便为未知数量的选项生成setter和getter方法。
请帮我解决这个问题......我从3天开始就为此奋斗....
提前致谢...
答案 0 :(得分:2)
好的,你需要的是class
名为Quiz
的东西:
public class Quiz
{
private String question;
private ArrayList<String> options; // no need to get separate variable for every option
public Quiz(String q, ArrayList<String> o)
{
super(); this.question = q; this.options = o;
}
//setter and getter.. like setOptions() getOptions() setQuestion() etc.. :/
}
现在让你的entirelist
Quiz
List
像这样:
List<Quiz> entirelist = new ArrayList<Quiz>();
现在将单个测验+选项传递给entirelist
,如下所示:
ArrayList<String> options = new ArrayList<String>();
options.add(op1);
options.add(op2);
options.add(op3);
entirelist.add(new Quiz(quest,options));
现在在适配器的getView()
中,删除所有选项'TextViews
,然后按照以下方式添加它们:
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.reviewrow, null);
}
Quiz currentQuiz = list11.get(position);
TextView question = (TextView) convertView.findViewById(R.id.textView2);
tvPhone.setText(currentQuiz.getquestion());
// now add options dynamically..
ArrayList<String> options = currentQuiz.getOptions();
for(String option : options)
{
TextView optionTextView = new TextView(context);
optionTextView.setText(option);
convertView.add(optionTextView);
}
return convertView;
}
答案 1 :(得分:0)
不要使用XML。
使用Java以编程方式将视图添加到布局中。然后,您可以动态选择适当数量的选项。
(几乎)Android中您可以用XML完成的所有事情,您可以使用Java代码。