我正在提取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数据并在文本视图中显示它。我需要的是根据resType在水平滚动视图中显示它们。所有重定型为“image”将在图像滚动视图中视频将在同一屏幕的视频滚动视图中
答案 0 :(得分: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传递给你的适配器。
这会让你知道如何去。