Android将项目添加到列表中
我想向我的listview添加一些json项。如何在java文件中添加项目 json示例:Android将项目添加到列表中,android,json,Android,Json,我想向我的listview添加一些json项。如何在java文件中添加项目 json示例: {"responseData": {"media":[{ "freshness":"1", "critic":"Roger Moore", "quote":"Adequate",
{"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"
}
}]}}
爪哇
stringurl=”http://site.com/";
ajax(url,JSONObject.class,新的AjaxCallback(){
@凌驾
公共无效回调(字符串url、JSONObject json、AjaxStatus状态){
if(json!=null){
JSONArray ja=json.optJSONObject(“responseData”).optJSONArray(“media”);
试试{
字符串测试=null;
对于(int i=0;i
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>
我想向我的listview添加一些json项。如何在java文件中添加项目?我建议您:
您需要一个扩展BaseAdapter或ArrayAdapter的适配器
- 从JSON字符串解析数据
- 添加到适配器
- 必须分析名为“responseData”的对象 JSONObject mResponseDataObject=新的JSONObject(您的JSONString)
- 解析JSONArray“媒体” JSONArray mmediarray=mResponseDataObject.getJSONArray(JSON_MEDIA_KEY);//JSON\u MEDIA\u KEY=“媒体”
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));
}
例如:
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;
}
}
公共类MyAdapter扩展了ArrayAdapter{
私人最终语境;
私人律师事务所;
公共mySimpleArrayaAdapter(上下文,ArrayList aMedias){
super(上下文、R.layout.rowlayout、值);
this.context=上下文;
this.mMedias=aMedias;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机充气(R.layout.list,父级,false);
TextView TextView=(TextView)rowView.findViewById(R.id.critic);
textView.setText(mMedias.get(position.getCritic());
// ….
返回行视图;
}
}
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;
}
}