I am new on Android and Java. Can any one tell me how can I load json result that i get from http request from URL to ListView. In below onpostExecute I get the result but have no idea how to load it on a listview.
protected void onPostExecute(String result) {
try {
JSONObject json = new JSONObject(result);
etResponse.setText(json.getString("name"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Here is my ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@+id/test_list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
For an example my Json looks like this:
{
id: "1",
name: "Any name",
email: "Any Email"
}
Thank you.
Your json data that you converted to string must be different things as for ex. blog posts so what you can do is convert into into a jsonArray and loop over the array grabbing each json object from array and assigning the values to listview. Do you need the actual code or the logic?
String jsonres = '{
id: "1",
name: "Any name",
email: "Any Email"
}';
You would do something like:
ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
JSONObject myJsonObject = new JSONObject(jsonres);
ListView mylv = (ListView) findViewById(R.id.mylvID);
HashMap<String, String> item = new HashMap<String, String>();
Sring id = myJsonObject.getString("id");
String name = myJsonObject.getString("name");
String email = myJsonObject.getString("email");
item.put("id", id);
item.put("name", name);
item.put("email", email);
items.add(item);
String[] keys = {"id", "name", "email"};//our keys for which we take the corresponding value
int[] ids = {R.id.title, R.id.author, R.id.date};
SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.list_view_item, keys, ids);
mylv.setAdapter(adapter);
//list_view_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainListActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.85"
android:gravity="left">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/blogPostImage"
android:src="@drawable/ic_launcher"/>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.2"
android:gravity="right">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text is a very large and long text"
android:id="@+id/title" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Author's Name"
android:id="@+id/author"
android:layout_weight="0.7"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="2014-15-16 14:15:16"
android:id="@+id/date"
android:layout_weight="0.3"
android:gravity="right"/>
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
</RelativeLayout>
I also suggest reading more about listadapters as that will help you understand what you are doing better.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.