Reputation: 397
I am working on quiz application. It contains 2 types of tests. The first test contains questions with 3 options fixed. *The second test contains questions with options not fixed. i.e the options may be 4 or 5 or 6 based on the question.* After the test I need to display the review page. Here is the review page code for the first type of test with fixed options.(Review page should contain the questions displayed for the test at that time)
My Code:
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) {
}
}
Now my problem is for the first test as we know the number of options are 3 so I have written the setter and getter methods for 1 question and 3 options and it is working fine. But for the second type of test how can I write the mainlist.java class for generating the setter and getter methods for unknown number of options.
Please help me regarding this...I am struggling for this since 3 days....
Thanks in advance...
Upvotes: 0
Views: 1827
Reputation: 37729
Ok what you will need is to make a class
named Quiz
something like this:
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.. :/
}
now in make your entirelist
a Quiz
List
like this:
List<Quiz> entirelist = new ArrayList<Quiz>();
and now pass single quiz + options to entirelist
like this:
ArrayList<String> options = new ArrayList<String>();
options.add(op1);
options.add(op2);
options.add(op3);
entirelist.add(new Quiz(quest,options));
now in adapter's getView()
, remove all options' TextViews
and add them dnamically something like this:
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;
}
Upvotes: 2
Reputation: 4993
Don't use XML.
Add the Views to the layout programatically, in Java. Then you can pick up the appropriate number of options dynamically.
(Almost) Everything in Android that you can do in XML, you can do in Java code.
Upvotes: 0