问题
I want to add row to listview
from my JSON
response. Here is the code from which I'm getting JSON
and printing them in the cosole:
HttpClient hClient = new DefaultHttpClient();
HttpGet hGet = new HttpGet(
"APIHere");
ResponseHandler<String> rHandler = new BasicResponseHandler();
data = hClient.execute(hGet, rHandler);
JSONObject rootObj = new JSONObject(data);
JSONObject searchObj = rootObj.getJSONObject("searchdata");
JSONArray titlesObj = searchObj.getJSONArray("titles");
JSONArray descsObj = searchObj.getJSONArray("desc");
JSONArray linksObj = searchObj.getJSONArray("links");
for (int i = 0; i < titlesObj.length(); i++) {
String title = titlesObj.getString(i);
System.out.println("Titles: " + title);
}
for (int i = 0; i < descsObj.length(); i++) {
String desc = descsObj.getString(i);
System.out.println("Desc: " + desc);
}
for (int i = 0; i < linksObj.length(); i++) {
String link = linksObj.getString(i);
System.out.println("Link: " + link);
}
I'm iterating the whole JSONArray
and I'm able to print those in the console. Now, I want to put these response in the listview. I'm not getting any clue regarding this.
Any kind of help will be appreciated.
回答1:
For fetching the array items and show it into a listview:
ArrayList titles = new ArrayList();
ArrayList descs= new ArrayList();
ArrayList links= new ArrayList();
for (int i = 0; i < titlesObj.length(); i++) {
String title = titlesObj.getString(i);
titles.add(title);
System.out.println("Titles: " + title);
}
for (int i = 0; i < descsObj.length(); i++) {
String desc = descsObj.getString(i);
descs.add(title);
System.out.println("Desc: " + desc);
}
for (int i = 0; i < linksObj.length(); i++) {
String link = linksObj.getString(i);
links.add(title);
System.out.println("Link: " + link);
}
Next you make this arraylist a source to a listview like this:
// Get a handle to the list views
//get your instance of the listview for titles
ListView lvTitle = (ListView) findViewById(R.id.ListView01);
lv.setAdapter(new ArrayAdapter<string>((Your activity class).this,
android.R.layout.simple_list_item_1, titles ));
//get your instance of the listview for descriptions
ListView lvDesc = (ListView) findViewById(R.id.ListView02);
lv.setAdapter(new ArrayAdapter<string>((Your activity class).this,
android.R.layout.simple_list_item_1, descs));
//get your instance of the listview for links
ListView lvLinks = (ListView) findViewById(R.id.ListView03);
lv.setAdapter(new ArrayAdapter<string>((Your activity class).this,
android.R.layout.simple_list_item_1, links));
Edit
Whatever I said that should already solve your question. But, I can see that you are using the same activity to communicate to a remote server to get data. I suggest it would be best if you make a separate class for this which would return the json text data
. Then you can call this class from your activity to get data and set the list views in your activity. It would avoid unnecessary lags and force closes in your app.
Update
You need to implement a custom adapter for that. You need to define a single listitem.xml
with Layouts according to your requirement. Then inflate it to the list view.
Follow this tutorial.
Sample Row:
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail product image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/someImage"/>
</LinearLayout>
<!-- Your title-->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="Big Title"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<!-- Your subtitle -->
<TextView
android:id="@+id/subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="Smaller sub title" />
<!-- Rightend info -->
<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/title"
android:gravity="right"
android:text="info"
android:layout_marginRight="5dip"
android:textSize="10dip"
android:textColor="#10bcc9"
android:textStyle="bold"/>
<!-- Rightend Arrow -->
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
which would make your list row look like:

回答2:
HttpClient hClient = new DefaultHttpClient();
HttpGet hGet = new HttpGet(
"APIHere");
ResponseHandler<String> rHandler = new BasicResponseHandler();
data = hClient.execute(hGet, rHandler);
JSONObject rootObj = new JSONObject(data);
JSONObject searchObj = rootObj.getJSONObject("searchdata");
JSONArray titlesObj = searchObj.getJSONArray("titles");
JSONArray descsObj = searchObj.getJSONArray("desc");
JSONArray linksObj = searchObj.getJSONArray("links");
String[] a = new String[titlesObj.length()];
String[] b = new String[descsObj.length()];
String[] c = new String[linksObj.length()];
for (int i = 0; i < titlesObj.length(); i++) {
String title = titlesObj.getString(i);
a[i] = title;
}
for (int i = 0; i < descsObj.length(); i++) {
String desc = descsObj.getString(i);
b[i] = desc;
}
for (int i = 0; i < linksObj.length(); i++) {
String link = linksObj.getString(i);
c[i] = link;
}
ArrayList<String> al=new ArrayList<String>();
//if three jsonarrays having same length
for(i=0;i<linksObj.length();i++)
{
al.add(" " +a[i]+" " +b[i]+""+c[i]+"");
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>this,android.R.layout.simple_list_item_1,al);
ListView lv=(ListView)findViewById(R.id.listview);
lv.setAdapter(adapter);
}
回答3:
1.Create a POJO as per your need/response you are getting
public class SearchData {
private String title;
private String Description;
private String link;
//getter & setter methods for each field
}
2.Create a List<SearchData>
object and populate it by parsing response
List<SearchData> list = new ArrayList<SearchData>();
//parse JSON array to SearchData object and add it to list
list.add(searchDataObject);
3a. If you are willing to display a title only (i.e. if simple layout is enough)
ArrayAdapter<SearchData> adapter = new ArrayAdapter<Product>(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
** You also need to override the toString()
method of SearchData
POJO to return title i.e.
public class SearchData {
....
@Override
public String toString() {
return title;
}
}
3b. On the other hand if you are willing to use custom layout, then your need to extend an ArrayAdapter
and override getView
method, then bind the new adapter the same way we did in step 3a, except this time replacing ArrayAdapter
with custom adapter
Resources:
1 Parsing JSON response to ListView as suggested by Ram Kiran : http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/
2 Customizing ListView and using Custom Adapter : http://www.ezzylearning.com/tutorial.aspx?tid=1763429
来源:https://stackoverflow.com/questions/15131306/fix-jsonresponse-on-listview