Android : How to set json response to views programatically in android

on Thursday, August 14, 2014


I have created an app in which I am fetching data from server. So, I have used json parsing in my code.I am successfully parsed my json from server.Actually I want to make a questions and options base quiz application. So that, i have five entries in my database named question_id, question_title, option 1, option 2, option 3, option 4.I want to display all these widgets at run time.I am getting json response right.But I am displaying my data.Please help.. Here is my QuizActivity file



package com.aquib.quiz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity {

// Progress Dialog
private ProgressDialog pDialog;


// Creating JSON Parser object
JSONParser jParser = new JSONParser();

// url to get all products list
private static String url_all_products = "http://labs.kamkazi.com/quiz/quiz.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "quiz";
private static final String TAG_QUESTIONID = "question_id";
private static final String TAG_QUESTIONTITLE = "question_title";
private static final String TAG_OPTION1 = "option1";
private static final String TAG_OPTION2 = "option2";
private static final String TAG_OPTION3 = "option3";
private static final String TAG_OPTION4 = "option4";

// products JSONArray
JSONArray products = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);

// Loading products in Background Thread
new LoadAllProducts().execute();

}

/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {

/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(QuizActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}

/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET",
params);

// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());

try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);

// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);

// Storing each json item in variable
String questionTitle = c.getString(TAG_QUESTIONTITLE);
String option1 = c.optString(TAG_OPTION1);
String option2 = c.optString(TAG_OPTION2);
String option3 = c.optString(TAG_OPTION3);
String option4 = c.optString(TAG_OPTION4);
String questionid = c.optString(TAG_QUESTIONID);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value

map.put(TAG_QUESTIONID, questionid);
map.put(TAG_QUESTIONTITLE, questionTitle);
map.put(TAG_OPTION1, option1);
map.put(TAG_OPTION2, option2);
map.put(TAG_OPTION3, option3);
map.put(TAG_OPTION4, option4);

}
} else {
Toast myToast = Toast.makeText(getApplicationContext(),
"No quiz found", Toast.LENGTH_SHORT);
myToast.show();
}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();

/**
* Updating parsed JSON data into ListView
* */
LinearLayout myLayout = new LinearLayout(getBaseContext());
myLayout.setOrientation(LinearLayout.VERTICAL);


TextView myView = new TextView(getBaseContext());
myView.setText(TAG_QUESTIONTITLE);
myView.setPadding(5, 10, 0, 0);

myLayout.addView(myView);

}

}
}


Please also help me in creating radio buttons programatically. and It is my humble request not to do downvote my question.As i am a beginner.


0 comments:

Post a Comment