2013-07-28 184 views
-1

我想添加一些json項目到我的列表視圖。我怎樣才能在java文件中添加項目?Android將項目添加到列表

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文件中添加項目?

回答

1

我建議你:

您需要延伸BaseAdapter或ArrayAdapter

  • 解析數據從JSON字符串
  • 一個適配器添加到適配器

如何分析數據?

有了您的JSON字符串:

  • 您必須分析其中有名稱的對象: 「responseData」

    的JSONObject mResponseDataObject =新的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)); 
} 

塔後你將創建新的實例Adapter的實例。

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; 
    } 
}