大家好,我正在尝试解析 Json 数据并尝试将其显示在 ListView 中。自过去 8 小时以来一直在尝试此操作,但无法尝试。基本上我不想对不同的值使用不同的 ArrayList(JSONObjects)。我想将值存储在 Object 使用数据模型中,并将该模型中的值获取到自定义 ArrayAdapter 中,并将其膨胀到 ListView 中。
现在的问题:列表中未显示任何内容。只是一个空白活动。
下面是我的代码。我不粘贴XML,因为它非常简单的列表视图和1个带有图像和2个文本视图的list_item。
凌空3.爪哇
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);
}
}
凌空3数据模型.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;
}
}
凌空3列表适配器.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 中,要么将值传递到模型中并从那里获取值。只是想提高我使用模型、泛型和单个对象的技能。
提前致谢
在 try 块的末尾移动这些行。
原因:您正在尝试创建适配器,而dataModel
中没有数据。只有在从网络调用返回 json 后,它才可用。
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();
附言按照代码的编写方式,它只会显示一个项目。您需要使用模型数组并将其传递给适配器。在适配器的getView
方法中使用位置变量获取要在列表视图中呈现的正确值。