0

I'm facing a problem in Next button while displaying the next question.

In my first time set text I'm getting the correct question and matched four option answers. What I needed is.

I have a Next button for displaying the next question and answers. When I click on next button I can get the next four options for next question. But I can't get next question after four times I click on next it shows next question, but answers are correct.

What I'm doing wrong? what I'm missing?

Any help would be appreciated.

    protected String doInBackground(String... Args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
    System.out.println("cool");
params.add(new BasicNameValuePair("tid", tid));
json = jsonParser.makeHttpRequest(url_get_quesurl, "GET", params);
System.out.println("ques value got");
Log.d("All Groups: ", json.toString());
    System.out.println("question");
try {
int success = json.getInt(TAG_SUCCESS);
System.out.println("Success");
if (success == 1) {
System.out.println("Success");
groups = json.getJSONArray(TAG_GROUP);
System.out.println("Result Success+++"+groups);
for (int i = 0; i < groups.length();i++) {
JSONObject c = groups.getJSONObject(i);
      String question = c.getString(TAG_QUES);
  System.out.println("Checking ::"+question);
ques1.add(question);
String answer = c.getString(TAG_ANSW);
System.out.println("Checking ::"+answer);
answ1.add(answer);
       }
    } else {
        showAlert();
    }
} catch (JSONException e) {
    System.out.println("Error "+e.toString());
}
return null;
     }  
     protected void onPostExecute(String file_url) {
     pDialog.dismiss();
     ques1=new ArrayList<String>(new ArrayList<String>(ques1));
        //  j=0;
        TextView txtque = (TextView) findViewById(R.id.que_txt); 
        txtque.setText(ques1.get(j));                      // here im getting the question
                                                 //   and answers in radiobutton text
        answ1=new ArrayList<String>(new ArrayList<String>(answ1));
        btn_practice1.setText(answ1.get(0));
        btn_practice2.setText(answ1.get(1));
        btn_practice3.setText(answ1.get(2));
        btn_practice4.setText(answ1.get(3));
        btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) { 
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
               // Toast.makeText(Question.this, "" + radioButton.getText(), 2000).show(); 
                TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
                txtRadio.setText("" + radioButton.getText());
            }
});
    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));              // here im not getting the next question
                                            //   but i can get the next four options in              
                                            //    radiobutton text
    k++;
    btn_practice1.setText(answ1.get((k*4)+0));
    btn_practice2.setText(answ1.get((k*4)+1));
    btn_practice3.setText(answ1.get((k*4)+2));
    btn_practice4.setText(answ1.get((k*4)+3));
     }
   });
    Button previousbtn = (Button) findViewById(R.id.prv_btn);
previousbtn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
j--;
TextView txtque = (TextView) findViewById(R.id.que_txt); 
txtque.setText(ques1.get(j));              // here also im not getting the perivous question
                                        //     but i cant get perivous four options in 
                                       //      radiobutton text
k--;
btn_practice1.setText(answ1.get((k*4)+0));
btn_practice2.setText(answ1.get((k*4)+1));
btn_practice3.setText(answ1.get((k*4)+2));
btn_practice4.setText(answ1.get((k*4)+3));
  }
});
  }
Peter O.
  • 32,158
  • 14
  • 82
  • 96
God'sGrace
  • 171
  • 1
  • 5
  • 19

1 Answers1

2

I assume, you are trying to manage question and its options on next/previous buttons click.

I achieved in my one of app same as you want:

Step1@ take one int count=0 define globally.

Step2@ when activity is called initially first question will be display with options.

setValues(count); // here I am setting question and options to textview inside setValues named method.

Step3@ onClick of NextButton check condition something like below:

if (count == question.size() - 1) {
    finish();
    break;
} 

else {
count++;
setValues(count);
   }

Step4@ onClick of PreviousButton check condition something like below:

if (count <= 0) {
Toast.makeText(OnPurposeUPSQuesAcitivty.this, "First Record",Toast.LENGTH_SHORT).show();
break;
} else {
count--;
setValues(count);
}

Your setValue method something look like below:

public void setValues( int j) {
txtque.setText(ques1.get(j));            

btn_practice1.setText(answ1.get(j);
btn_practice2.setText(answ1.get(j);
btn_practice3.setText(answ1.get(j);
btn_practice4.setText(answ1.get(j);
}

...........

Last Point@ After review your code, I don't came across that how you are setting values to options.

this

btn_practice1.setText(answ1.get((k*4)+0));

Will you explain above line of code, will update my answer, else you can use my solution as I mention above steps wise.

RobinHood
  • 10,897
  • 4
  • 48
  • 97
  • ok i try...and say btn_practice1.setText(answ1.get(k*4)+0); Its means in my answ1 ArrayList it contains 16 options for 4 question each 4 options so when increment i should display next four option so i used above line code for next four options.... – God'sGrace Feb 01 '13 at 07:41
  • I tried...i didnt get next question..Can you edit your idea in my code itself..please help me im new to android..What im doing wrong i dont know.. – God'sGrace Feb 01 '13 at 07:53
  • @God'sGrace but why you are getting position like `(k*4)+0);` this? – RobinHood Feb 01 '13 at 07:56
  • First time it displays 0 to 3 elements next when increment "k" becomes 1, so now k=1. but i need 4 to 7. so i done (1*4)+0); so now it displays 4th element...can you understand... – God'sGrace Feb 01 '13 at 07:59
  • Only problem is im not getting next question..After i click four times its displays next question...but answers are coming correct..please help me..plz implement your idea in my code, edit and say plz..really i dont know where im getting wrong... – God'sGrace Feb 01 '13 at 08:01
  • I tried again i click four times only its showing next question.. same as in perivous also..but answers are coming correct...in both next and perivous... – God'sGrace Feb 01 '13 at 08:11
  • vote up my question means u given vote down...change and vote...frnd – God'sGrace Feb 04 '13 at 09:37
  • @God'sGrace I didnt given, neither I upvote nor downvoted.BTW! why you need to up your question? Its not a cross agreement, that you upvoted so I also upvote, if I find your question indeed need deserve up then sure I will, but you can't request/force. – RobinHood Feb 04 '13 at 09:42