2016-03-04 49 views
0

我獲取JSON數據如下如何在滾動視圖中放置不同的JSON對象?

  JsonObjectRequest jor = new JsonObjectRequest(url, null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 

        try{ 

         JSONArray ja = response.getJSONArray("results"); 

         for(int i=0; i < ja.length(); i++){ 

          JSONObject jsonObject = ja.getJSONObject(i); 

          int id = Integer.parseInt(jsonObject.optString("id").toString()); 
          String url = jsonObject.getString("resLink"); 
          String resType= jsonObject.getString("resType"); 
          String name = jsonObject.getString("resName"); 

          data += "ResType= "+resType +" \n URl= "+ url +" \n id = "+ id + "\n Name = "+ name+"\n\n\n" ; 
          // data +=videoUrl+url; 

         } 

         output.setText(data); 


        }catch(JSONException e){e.printStackTrace();} 
       } 

我只是獲取JSON數據和表示在文本它view.What我需要的是根據resType.Means顯示那些在水平滾動視圖所有restype「圖像」將在圖像滾動視圖和視頻中,視頻將會滾動視圖在同一屏幕

+0

你好,你的代碼我可以說,data + =「ResType =」+ resType +「\ n URl =」+ url +「\ n id =」+ id +「\ n Name =」+ name +「\ n \ n \ n「;將添加數據字符串中的所有數據,您不區分基於資源類型的所有數據,嘗試獲取四個不同的數據字符串,並區分所有數據並將它們添加到每個個體中,並在textview中顯示它們。 –

+0

把它們當成你正在做的簡單的JSON與其他視圖。 –

+0

好吧@Silvans讓我試試... –

回答

0

加上一個模型這樣

/** 
    * Created by waqas.ali on 3/4/2016. 
    */ 
    public class MyModel { 
    String name; 
    String resType; 
    String url; 
    int id; 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getResType() { 
     return resType; 
    } 
    public void setResType(String resType) { 
     this.resType = resType; 
    } 
    public String getUrl() { 
     return url; 
    } 
    public void setUrl(String url) { 
     this.url = url; 
    } 
} 

然後更換您的代碼

JSONArray ja = response.getJSONArray("results"); 
    List<MyModel> myModelList = new ArrayList<MyModel>; 
    for(int i=0; i < ja.length(); i++){ 

    JSONObject jsonObject = ja.getJSONObject(i); 
    MyModel mymodel=new MyModel(); 
    mymodel.id = Integer.parseInt(jsonObject.optString("id").toString()); 
    mymodel.url = jsonObject.getString("resLink"); 
    mymodel.resType= jsonObject.getString("resType"); 
    mymodel.name = jsonObject.getString("resName"); 

    myModelList.add(mymodel); 

    } 

然後終於通myModelList到您的適配器。

這會給你想法如何去。

+0

你可以檢查這一行顯示編譯時錯誤列表 myModelList = new ArrayList ; –

+0

有沒有想過().. –

+0

對不起,其列表 myModelList = new ArrayList (); –

相關問題