我想在列表视图中添加一些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"
}
}]}}
的java
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
});
list.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文件中添加项目?
答案 0 :(得分:1)
我建议你:
您需要一个扩展BaseAdapter或ArrayAdapter
的适配器如何解析数据?
使用您的JSON字符串:
您必须解析名称为“responseData”
的对象JSONObject mResponseDataObject = new JSONObject(yourJSONString);
解析JSONArray“media”
JSONArray mMediaArray = mResponseDataObject.getJSONArray(JSON_MEDIA_KEY); // JSON_MEDIA_KEY =“media”
在mMediaArray的每个项目中 - &gt;你应该解析一个对象
class Media {
private int freshness;
private String critic;
private String quote;
private String publication;
private Date date;
// Constructors, Getters & Setters
}
你必须通过这种方式解析:
ArrayList<Media> mMedias = new ArrayList<Media>();
for (int i = 0; i < mMeidaArray.length(); i++) {
JSONObject mMediaObject = mMediaArray.getJSONObject(i);
int freshness = mMediaObject.getInt(MEDIA_FRESHNESS); // MEDIA_FRESHNESS = "freshness";
//....
mMedias.add(new Media(freshness, critic, quote, publication, date));
}
之后,您将创建适配器实例的新实例。
MyAdapter mAdapter = new MyAdapter(getApplicationContext(), mMedias);
yourListView.setAdapter(mAdapter);
示例:
public class MyAdapter extends ArrayAdapter<Media> {
private final Context context;
private ArrayList<Media> mMedias;
public MySimpleArrayAdapter(Context context, ArrayList<Media> aMedias) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.mMedias = aMedias;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.critic);
textView.setText(mMedias.get(position).getCritic());
// ….
return rowView;
}
}