2016-03-04 47 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 (); –

相关问题