Reputation: 469
Hi guys I am trying to parse Json data and trying to show it into a ListView. Trying this since last 8hrs but not been able to. Basically I dont want to use different ArrayList for different values(JSONObjects). I want to store the values in and Object use a Data Model and get the values from that model into the Custom ArrayAdapter and inflate it into the ListView.
Issue right now : nothing is being displayed in the list. Just a Blank Activity.
Below is my code. I am not pasting XML as that is pretty simple ListView and 1 list_item with an image and 2 text views.
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);
}
}
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;
}
}
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;
}
}
I know there are many posts related to displaying data into the ListView using JSON but my issue is either to pass an Object containing all the 3 String values into an ArrayLisy or pass values into the Model and get the values from there. Just trying to increase my skills of using the Models, Generics and single Object.
Thanks in advance
Upvotes: 0
Views: 960
Reputation: 18112
Move these lines at the end of your try block.
Reason: You are trying to create your adapter when there is no data in dataModel
. It will be available only after json is returned from the network call.
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();
P.S. The way your code has been written it would display only one item. You need to use an array of your model and pass that to the adapter. In adapter's getView
method use position variable to get the correct value to be rendered in the list view.
Upvotes: 1