I’m new to android. I’m doing a project on quiz app. I have problem on displaying the question and answers.
My problem is that I have stored the question and answers in the array. The question and answer is displaying but not in order.
I got the question and answers from database. They are coming in order but in my array list it’s not in order. The question and answer are not matching.
How to display it in correct order for question by matching correct order.
Please help me and thanks a lot in advance.
code
protected void onPostExecute(String file_url) {
pDialog.dismiss();
ques1=new ArrayList<String>(new HashSet<String>(ques1));
System.out.println(" Question array:"+ques1);
String[] quesArr = new String [ques1.size()];
quesArr = ques1.toArray(quesArr);
for(String s: quesArr){
Collections.sort(ques1, new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
// TODO Auto-generated method stub
return 0;
}
});
System.out.println("All Array Questions:"+s);
final TextView txtque = (TextView) findViewById(R.id.que_txt);
final String text=ques1.get(j).toString();
System.out.println("Array:"+text);
txtque.setText(text);
}
try {
int i = 0;
// answ1=new ArrayList<String>(new HashSet<String>(answ1));
JSONObject c = groups.getJSONObject(i);
String answer = c.getString(TAG_ANSW);
System.out.println("Checking ::"+answer);
answ=answer;
HashMap<String, String> map1 = new HashMap<String, String>();
answ1.add(answer);
System.out.println(" Answer array:"+answ1);
String[] answArr = new String [answ1.size()];
answArr = answ1.toArray(answArr);
for(String A: answArr){
Collections.sort(answ1, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
System.out.println("All Array Answers:"+A);
}
final TextView txtRadio = (TextView) findViewById(R.id.rdtxt);
btn_practicerg =(RadioGroup) findViewById(R.id.rdgroup);
btn_practice1 = (RadioButton) findViewById(R.id.RB1);
btn_practice1.setText(String.valueOf(answArr[0]));
btn_practice2 = (RadioButton) findViewById(R.id.RB2);
btn_practice2.setText(String.valueOf(answArr[1]));
btn_practice3 = (RadioButton) findViewById(R.id.RB3);
btn_practice3.setText(String.valueOf(answArr[2]));
btn_practice4 = (RadioButton) findViewById(R.id.RB4);
btn_practice4.setText(String.valueOf(answArr[3]));
btn_practicerg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton checkedRadioButton = (RadioButton) findViewById(checkedId);
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
btn_practicerg.clearCheck();
Button nextBtn = (Button) findViewById(R.id.nxt_btn);
nextBtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
j++;
TextView txtque = (TextView) findViewById(R.id.que_txt);
txtque.setText(ques1.get(j).toString());
k++;
btn_practice1.setText(answ1.get(k).toString());
k++;
btn_practice2.setText(answ1.get(k).toString());
k++;
btn_practice3.setText(answ1.get(k).toString());
k++;
btn_practice4.setText(answ1.get(k).toString());
}
});
}
You’re storing the questions in a HashSet, which is not preserving the order. Plus, your comparator always returns 0.