I have created an app which fetches data from a URL using a JSON array request when I display the data from URL like this:
how can I show this data in a listview? Here is my code:
public class JsonRequestActivity extends Activity implements OnClickListener {
private String TAG = JsonRequestActivity.class.getSimpleName();
private Button btnJsonObj, btnJsonArray;
private TextView msgResponse;
private ProgressDialog pDialog;
// These tags will be used to cancel the requests
private String tag_json_obj = "jobj_req", tag_json_arry = "jarray_req";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
btnJsonObj = (Button) findViewById(R.id.btnJsonObj);
btnJsonArray = (Button) findViewById(R.id.btnJsonArray);
msgResponse = (TextView) findViewById(R.id.msgResponse);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
btnJsonObj.setOnClickListener(this);
btnJsonArray.setOnClickListener(this);
}
private void showProgressDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideProgressDialog() {
if (pDialog.isShowing())
pDialog.hide();
}
/**
* Making json object request
* */
private void makeJsonObjReq() {
showProgressDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
Const.URL_JSON_OBJECT, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Androidhive");
params.put("email", "abc@androidhive.info");
params.put("pass", "password123");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq,
tag_json_obj);
// Cancelling request
// ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);
}
/**
* Making json array request
* */
private void makeJsonArryReq() {
showProgressDialog();
JsonArrayRequest req = new JsonArrayRequest(Const.URL_JSON_ARRAY,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req,
tag_json_arry);
// Cancelling request
// ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_arry);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnJsonObj:
makeJsonObjReq();
break;
case R.id.btnJsonArray:
makeJsonArryReq();
break;
}
}
}
And this is the URL I am using
http://api.androidhive.info/volley/person_array.json
Your response is like this:
Here it is json array. You can parse it this way:
Create a java object class. Your class is like this:
Your Phone Entity is here:
Your User object is it:
Now in your
onResponse
callback use this line of code:Now you have UserObject List in
yourList
. Next stage is create a ListView and initialize it. Create an adapter , set it's item layout and invalidate data of it's item.Suppose your ListView is like this:
and your list item row like this:
Now Your Adapter will like this:
Now you just call this way in
onResponse()
callbackEdit: Why Java Object
You have a json Array. It's individual object is like this:
Here, it is a json object, it contains string's and tag's are "name", 'email', and an Json Object tag "phone".
So here this object contains some string and a json object name "phone".
From previous discussion, I created a Java Object name
PhoneObject
it's tag's are initialized using this way:Now "phone" tag is in main Object. So i created
UserObject
and initializedPhoneObject
in that object.Note: You have to realize the json structure and after that you have to start creating the java object.
All are done. If need more information visit here
You have to write some code for this.
you can take help from below links:
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
http://www.vogella.com/tutorials/AndroidListView/article.html
You can use :
helpful link : https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView