-1

我想在我的列表视图中添加一些 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 文件中添加项目?

4

1 回答 1

1

我建议你:

您需要一个扩展 BaseAdapter 或 ArrayAdapter 的适配器

  • 从 JSON 字符串解析数据
  • 添加到适配器

如何解析数据?

使用您的 JSON 字符串:

  • 您必须解析具有名称的对象:“responseData”

    JSONObject mResponseDataObject = new JSONObject(yourJSONString);

  • 解析 JSONArray “媒体”

    JSONArray mMediaArray = mResponseDataObject.getJSONArray(JSON_MEDIA_KEY); // JSON_MEDIA_KEY = "媒体"

在 mMediaArray 中的每个项目中 -> 您应该解析为一个对象

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;
  }
} 
于 2013-07-28T11:55:46.420 回答