Ich bin Neuling auf Android und es eine App entwerfen, die Fragen und Optionen aus einer JSON-Datei lesen und Text in der Textansicht festlegen wird, und die nächste Frage wird nur angezeigt, wenn Die richtige Option wird angeklickt, aber ich kann nicht herausfinden, wie ich das machen soll.Parsing json Inhalt und Anzeigen in Text View
{
"questions": [
{
"question": "Question....",
"opt1": "[email protected]",
"opt2": "country",
"opt3" : "male",
"opt4": "option 4",
"coropt": "b"
},
{
"question": "Question....",
"opt1": "[email protected]",
"opt2": "country",
"opt3" : "male",
"opt4": "option 4",
"coropt": "a"
},
{
"question": "Question....",
"opt1": "[email protected]",
"opt2": "country",
"opt3" : "male",
"opt4": "option 4",
"coropt": "d"
},
{
"question": "Question....",
"opt1": "[email protected]",
"opt2": "country",
"opt3" : "male",
"opt4": "option 4",
"coropt": "c"
}
]
}
here is the view in which json array data needs to be placed one by one
Dies ist der Code arbeite ich an, aber ich bin fest, wie und wo JSON-Daten zu analysieren.
public class JavaQuiz extends AppCompatActivity {
TextView ltv, tv1, tv2, tv3, tv4;
// JSON Node names
private static final String TAG_QUESTIONS = "questions";
private static final String TAG_QUESTION = "question";
private static final String TAG_OPTION1 = "opt1";
private static final String TAG_OPTION2 = "opt2";
private static final String TAG_OPTION3 = "opt3";
private static final String TAG_OPTION4 = "opt4";
private static final String TAG_CORRECTOPTION = "coropt";
// contacts JSONArray
JSONArray questions = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> questionList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java_quiz);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ltv = (TextView) findViewById(R.id.textView7);
tv1 = (TextView) findViewById(R.id.textView8);
tv2 = (TextView) findViewById(R.id.textView9);
tv3 = (TextView) findViewById(R.id.textView10);
tv4 = (TextView) findViewById(R.id.textView11);
}
class QuestionShow extends AsyncTask<String, Void, Void> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(JavaQuiz.this);
progressDialog.setTitle("Fetching");
progressDialog.setMessage("Setting question");
progressDialog.show();
}
@Override
protected Void doInBackground(String... params) {
HttpClient client = new DefaultHttpClient();
/* HttpPost post=new HttpPost("http://192.168.1.5/questions.json");
post.setEntity(new UrlEncodedFormEntity(params[0]));
HttpResponse response = client.execute(post);
*/
String jsonStr = "http://192.168.1.5/questions.json";
/* if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
questions = jsonObj.getJSONArray(TAG_QUESTIONS);
// looping through All questions
for (int i = 0; i < questions.length(); i++) {
JSONObject q = questions.getJSONObject(i);
String question = q.getString(TAG_QUESTION);
String op1 = q.getString(TAG_OPTION1);
String op2 = q.getString(TAG_OPTION2);
String op3 = q.getString(TAG_OPTION3);
String op4 = q.getString(TAG_OPTION4);
String coropt = q.getString(TAG_CORRECTOPTION);
/* // tmp hashmap for single question
HashMap<String, String> ques = new HashMap<String, String>();
// adding each child node to HashMap key => value
ques.put(TAG_QUESTION, question);
ques.put(TAG_OPTION1, op1);
ques.put(TAG_OPTION2, op2);
ques.put(TAG_OPTION3, op3);
ques.put(TAG_OPTION4, op4);
ques.put(TAG_CORRECTOPTION, coropt);
// adding contact to contact list
questionList.add(ques);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
*/
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
String jsonStr = "http://192.168.1.5/questions.json";
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
questions = jsonObj.getJSONArray(TAG_QUESTIONS);
// looping through All questions
for (int i = 0; i < questions.length();) {
JSONObject q = questions.getJSONObject(i);
String question = q.getString(TAG_QUESTION);
String op1 = q.getString(TAG_OPTION1);
String op2 = q.getString(TAG_OPTION2);
String op3 = q.getString(TAG_OPTION3);
String op4 = q.getString(TAG_OPTION4);
String coropt = q.getString(TAG_CORRECTOPTION);
ltv.setText(question);
tv1.setText(op1);
tv2.setText(op1);
tv3.setText(op1);
tv4.setText(op1);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
public void answer(View view)
{
String
if (view==tv1)
{
if(tv1.getText().toString()==)
}
}
}
Mögliches Duplikat von [Wie JSON in Java analysieren] (http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) –
ich getan habe, dass die Listenansicht verwenden, aber Ich bin nicht in der Lage, sie in der Textansicht zu analysieren und die Frage zu ändern, wenn der Benutzer die richtige Option drückt. –
Zeigen Sie dann den Java-Code an, mit dem Sie arbeiten. Deine Frage ist vage, deshalb habe ich sie als Duplikat markiert. –