修复listview上的JSONResponse
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了修复listview上的JSONResponse相关的知识,希望对你有一定的参考价值。
我想从listview
回复中添加行到JSON
。这是我从中获取JSON
并在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);
}
我正在迭代整个JSONArray
,我可以在控制台中打印它们。现在,我想将这些响应放在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);
}
接下来,让这个arraylist成为listview的源代码,如下所示:
// 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));
编辑
无论我说什么,都应该已经解决了你的问题。但是,我可以看到您使用相同的活动与远程服务器通信以获取数据。我建议最好是为此创建一个单独的类,它将返回json文本data
。然后,您可以从活动中调用此类以获取数据并在活动中设置列表视图。它可以避免你的应用程序中不必要的滞后和强制关闭。
更新
您需要为此实现自定义适配器。你需要根据你的要求定义一个带有布局的listitem.xml
。然后将其膨胀到列表视图。
关注这个tutorial。
样本行:
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>
这将使您的列表行看起来像:
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);
}
1.根据您的需求/响应创建POJO
public class SearchData {
private String title;
private String Description;
private String link;
//getter & setter methods for each field
}
2.创建一个List<SearchData>
对象并通过解析响应来填充它
List<SearchData> list = new ArrayList<SearchData>();
//parse JSON array to SearchData object and add it to list
list.add(searchDataObject);
3A。如果你愿意只显示一个标题(即如果简单的布局就足够了)
ArrayAdapter<SearchData> adapter = new ArrayAdapter<Product>(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
**您还需要覆盖toString()
POJO的SearchData
方法以返回标题,即
public class SearchData {
....
@Override
public String toString() {
return title;
}
}
3B。另一方面,如果你愿意使用自定义布局,那么你需要扩展ArrayAdapter
并覆盖getView
方法,然后以与我们在步骤3a中相同的方式绑定新适配器,除非这次用自定义适配器替换ArrayAdapter
资源:
1按照Ram Kiran的建议解析对ListView的JSON响应:http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/
2自定义ListView并使用自定义适配器:http://www.ezzylearning.com/tutorial.aspx?tid=1763429
以上是关于修复listview上的JSONResponse的主要内容,如果未能解决你的问题,请参考以下文章
如何修复“所有单击的 Listview 项目都可以使用相同的数据”
如何修复 javafx 中的渲染错误(ComboBox、ListView)