【发布时间】:2014-12-26 21:18:21
【问题描述】:
大家好,我正在尝试解析 Json 数据并尝试将其显示到 ListView 中。自过去 8 小时以来一直在尝试此操作,但未能成功。基本上我不想为不同的值(JSONObjects)使用不同的 ArrayList。我想将值存储在对象中,并且使用数据模型并将该模型中的值获取到自定义 ArrayAdapter 中并将其膨胀到 ListView 中。
现在的问题:列表中没有显示任何内容。只是一个空白活动。
下面是我的代码。我没有粘贴 XML,因为这是非常简单的 ListView 和 1 个带有图像和 2 个文本视图的 list_item。
Volley3.Java
package com.droidacid.networkingdemo.http_using_volley;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.droidacid.networkingdemo.L;
import com.droidacid.networkingdemo.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Volley3 extends Activity {
ListView lvVolley3;
//ArrayList<Volley3DataModel> videoArray = new ArrayList<Volley3DataModel>();
Volley3ListAdapter videoAdapter;
Volley3DataModel dataModel;
String feedUrl = "https://gdata.youtube.com/feeds/api/users/slidenerd/uploads?v=2&alt=jsonc&start-index=1&max-results=10";
Context context = this;
String videoTitle, videoUploadedAt, videoThumbnailURL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.volley3);
lvVolley3 = (ListView) findViewById(R.id.lvVolley3);
callJson();
lvVolley3.setAdapter(videoAdapter);
}
private void callJson() {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, feedUrl, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
// We perform all the JSON operations in this method.
try {
// This gets a JsonArray which is inside a JsonObject
JSONArray jsonVideoArray = jsonObject.getJSONObject("data").getJSONArray("items");
for(int i = 0; i < jsonVideoArray.length(); i++) {
JSONObject jsonItems = jsonVideoArray.getJSONObject(i);
videoTitle = jsonItems.getString("title");
videoThumbnailURL = jsonItems.getJSONObject("thumbnail").getString("sqDefault");
videoUploadedAt = jsonItems.getString("uploaded");
//L.l("Titles : " + videoTitle + ". Url : " + videoThumbnailURL + " Uploaded At : " + videoUploadedAt);
dataModel = new Volley3DataModel(videoTitle, videoThumbnailURL, videoUploadedAt);
//videoArray.add(dataModel);
}
} catch (JSONException e) {
L.l("I am a Json Exception : " + e.toString());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
L.l("Error happened hoooo : " + volleyError.toString());
L.t(context, "Error happened hoooo : " + volleyError.toString());
}
}
);
videoAdapter = new Volley3ListAdapter(this, dataModel);
//Tell the videoAdapter that the List Data has been changed
//Telling the Adapter that the data has been changed
L.l("DataSet changing");
videoAdapter.notifyDataSetChanged();
VolleyApplication.getInstance().getRequestQueue().add(jsonObjectRequest);
}
}
Volley3DataModel.java
package com.droidacid.networkingdemo.http_using_volley;
public class Volley3DataModel {
private String title, url, updated;
Volley3DataModel(String title, String url, String updated) {
this.title = title;
this.url = url;
this.updated = updated;
}
public String getTitle() {
return title;
}
public String getUrl() {
return url;
}
public String getUpdated() {
return updated;
}
}
Volley3ListAdapter.java
package com.droidacid.networkingdemo.http_using_volley;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.droidacid.networkingdemo.L;
import com.droidacid.networkingdemo.R;
import com.squareup.picasso.Picasso;
public class Volley3ListAdapter extends ArrayAdapter<Volley3DataModel> {
ImageView ivThumb;
Volley3DataModel myData;
TextView tvTitle, tvUploaded;
Context context;
String title, url, uploaded;
//ArrayList<Volley3DataModel> myDataList;
public Volley3ListAdapter(Context context, Volley3DataModel dataModel) {
super(context, R.layout.volley3_list_youtube_item);
L.l("Volley3ListAdapter() constructor");
this.context = context;
this.myData = dataModel;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
L.l("I'm inside getView()");
if(convertView == null) {
L.l("ConvertView is null so inflating the convertView");
//convertView = LayoutInflater.from(context).inflate(R.layout.volley3_list_youtube_item, parent, false);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.volley3_list_youtube_item, parent, false);
}
L.l("Assigning values to the views");
ivThumb = (ImageView) convertView.findViewById(R.id.ivVolley3);
tvTitle = (TextView) convertView.findViewById(R.id.tvTitleVolley3);
tvUploaded = (TextView) convertView.findViewById(R.id.tvUploadedVolley3);
L.l("Loading the data into the Views");
Picasso.with(getContext()).load(myData.getUrl()).centerCrop().into(ivThumb);
tvTitle.setText(myData.getTitle());
tvUploaded.setText(myData.getUpdated());
return convertView;
}
}
我知道有很多帖子与使用 JSON 将数据显示到 ListView 相关,但我的问题是将包含所有 3 个字符串值的对象传递到 ArrayLisy 或将值传递到模型并从那里获取值。只是想提高我使用模型、泛型和单个对象的技能。
提前致谢
【问题讨论】:
-
模型总是更好。如果将来 Json 有一些变化,您只需更改您的模型。
-
是的,这就是为什么开始使用模型。我知道来自 Hibernate 的模型,但对 ListViews 并不满意,但现在能够理解它们。但卡在这个列表填充问题中。 :(
-
如果对你有帮助,请看我的回答。
-
一分钟后检查您的邮件。
-
检查邮件。它现在可以工作了。
标签: android json android-listview android-volley