if the question as json array like follwing
[{"questionId":"Q18868","questionValue":"What is the minimum age for obtaining an Alabama driver license?","choice1":"At least 16 years.","choice2":"At least 17 years.","choice3":"At least 18 years.","choice4":"At least 19 years.","answer":"1","imageName":"NA"},{"questionId":"Q18873","questionValue":"Whether Alabama license may be issued to persons under age 19 years, without secondary school graduation or current attendance?","choice1":"No.","choice2":"Yes.","choice3":"Yes, on special permission.","choice4":"Any thing else.","answer":"1","imageName":"NA"},{"questionId":"Q19001","questionValue":"For how many moving traffic violations on a GDL (graduated driver license), you license will be suspended?","choice1":"1 or more.","choice2":"2 or more.","choice3":"3 or more.","choice4":"4 or more.","answer":"2","imageName":"NA"},{"questionId":"Q19323","questionValue":"For which persons Graduated Driver License (GDL) system is not applicable?","choice1":"Persons aged 18 years or older.","choice2":"Persons aged 17 or older with valid driver license for 6 months.","choice3":"Persons aged 16 or older, who are married or head of a household.","choice4":"All the above persons.","answer":"4","imageName":"NA"},{"questionId":"Q19324","questionValue":"Whether persons who have been legally relieved of minority status, is eligible for GDL system?","choice1":"NO.","choice2":"Yes.","choice3":"Yes, if under 16 years.","choice4":"Any thing else.","answer":"1","imageName":"NA"},{"questionId":"Q19325","questionValue":"Who should accompany a person aged 15 years with learner\u0027s license in the vehicle, while driving?","choice1":"A parent or legal guardian.","choice2":"A licensed driver aged 21 years or older.","choice3":"A licensed or certified driving instructor.","choice4":"Any one of the above persons.","answer":"4","imageName":"NA"},{"questionId":"Q19326","questionValue":"In which seat of the vehicle, the person accompanying the driver aged 15 years with learner\u0027s license should sit?","choice1":"Seat next to the driver.","choice2":"Any front seat.","choice3":"Seat behind the driver.","choice4":"Any where else.","answer":"1","imageName":"NA"}]
JsonParser .java
package com.alabama;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
public class JsonParser {
String fileName;
Activity activity;
public JsonParser(Activity activity,String fileName) {
this.activity=activity;
this.fileName=fileName;
}
public ArrayList<QuestBeen> getQuestion() {
ArrayList<QuestBeen> list=new ArrayList<QuestBeen>();
JSONArray array;
try {
array = new JSONArray(loadJSONFromAsset());
for(int i=0;i<array.length();i++){
JSONObject json_data = array.getJSONObject(i);
QuestBeen been=new QuestBeen();
been.setQuestionId(json_data.getString("questionId"));
been.setQuestionValue(json_data.getString("questionValue"));
been.setChoice1(json_data.getString("choice1"));
been.setChoice2(json_data.getString("choice2"));
been.setChoice3(json_data.getString("choice3"));
been.setChoice4(json_data.getString("choice4"));
been.setAnswer(json_data.getString("answer"));
list.add(been);
}
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = activity.getAssets().open(fileName);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
reading files in the activity
package com.alabama;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class TestActivity extends Activity{
int ans=0;
int id=0;
int rightValue=0;
ArrayList<QuestBeen> question;
JsonParser parser;
TextView textViewQuestion,textViewHead;
RadioGroup group;
RadioButton choice1,choice2,choice3,choice4;
Button buttonPrev,buttonNext;
int pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
pos=getIntent().getExtras().getInt("position");
pos=10+pos;
parser=new JsonParser(TestActivity.this,"QC"+pos+".json");
System.out.println("--------------------------");
Log.d("json ", parser.loadJSONFromAsset().toString());
question=parser.getQuestion();
GUI();
}
private void GUI() {
textViewQuestion=(TextView)findViewById(R.id.textViewQuestion);
textViewHead=(TextView)findViewById(R.id.textViewHead);
group=(RadioGroup)findViewById(R.id.radioGroup1);
choice1=(RadioButton)findViewById(R.id.radio0);
choice2=(RadioButton)findViewById(R.id.radio1);
choice3=(RadioButton)findViewById(R.id.radio2);
choice4=(RadioButton)findViewById(R.id.radio3);
textViewHead.setText("1/"+question.size());
ConstantValues.totaQuestin=question.size();
buttonPrev=(Button)findViewById(R.id.button1);
buttonNext=(Button)findViewById(R.id.button2);
setVal();
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
int radioButtonID = group.getCheckedRadioButtonId();
View radioButton = group.findViewById(radioButtonID);
int idx = group.indexOfChild(radioButton);
if(idx==ans){
rightValue=1;
}
else
rightValue=0;
//Toast.makeText(getApplicationContext(), idx+"---", Toast.LENGTH_SHORT).show();
}
});
buttonNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ConstantValues.answers.add(id,rightValue);
id++;
Log.d("question", id+"--");
if(id<=question.size()-1)
setVal();
else{
Toast.makeText(getApplicationContext(), "Result", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(TestActivity.this,ResultActivity.class);
startActivity(intent);
}
Log.d("-----------", ConstantValues.answers.toString());
}
});
buttonPrev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
id--;
Log.d("question", id+"--");
if(id<=0){
id=0;
Toast.makeText(getApplicationContext(), "No Questions", Toast.LENGTH_SHORT).show();
}
else
setVal();
}
});
}
private void setVal() {
ans=Integer.parseInt(question.get(id).getAnswer());
group.clearCheck();
textViewHead.setText(id+"/"+question.size());
textViewQuestion.setText(question.get(id).getQuestionValue());
choice1.setText(question.get(id).getChoice1());
choice2.setText(question.get(id).getChoice2());
choice3.setText(question.get(id).getChoice3());
choice4.setText(question.get(id).getChoice4());
}
}