我想在我的列表视图中添加一些 json 项目。如何在 java 文件中添加项目?
json 示例:
{"responseData": {"media":[{
"freshness":"1",
"critic":"Roger Moore",
"quote":"Adequate",
"publication":"McClatchy-Tribune News Service",
"date":"2013-01-08"
},
{
"freshness":"1",
"critic":"Laremy Legel",
"quote":"You won't be upset you saw it, you'll have some fun, you'll see Wolvie beat the living hell out of a helicopter.",
"publication":"Film.com","date":"2010-07-07"
},
{
"freshness":"1",
"critic":"Sara Vilkomerson",
"quote":"Did the plot points stick in my head five minutes after leaving the theater? Not so much ... but I know I was having fun while watching.",
"publication":"New York Observer",
"date":"2009-05-06"
}
}]}}
爪哇
String url = "http://site.com/";
aq.ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject json, AjaxStatus status) {
if(json != null){
JSONArray ja = json.optJSONObject("responseData").optJSONArray("media");
try {
String test = null;
for(int i = 0 ; i < ja.length(); i++){
JSONObject jo = ja.optJSONObject(i);
test = jo.getString("critic");
Toast.makeText(aq.getContext(), test, Toast.LENGTH_LONG).show();
//add items to list//
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//end try
}//end if null
}//end callback
});
列表.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#FF800000"
>
</ListView>
list_body.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/freshness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/critic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/quote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold"/>
</LinearLayout>
我想在我的列表视图中添加一些 json 项目。如何在 java 文件中添加项目?