Panache Panache - 1 month ago 11
Android Question

Using Volley to get list from JSON generating list with last object only

I am trying to fetch data which is in JSON form. but the list created on fetching is having last object details only against every list item. pls guide .

Here is code for Mainactivity to fetch data.

package com.example.ankurdell.customlistview;


import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;

public class MainActivity extends Activity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();

// Movies json url

private static final String url = "https://api.dailymotion.com/videos";
private ProgressDialog pDialog;
private ArrayList<DailyMovie> movieList = new ArrayList<>();
private ListView listView;
private CustomListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);

pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();


// Creating volley request obj
JsonObjectRequest movieReq = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
hidePDialog();

// Parsing json

try {

JSONArray jsonArray = response.getJSONArray("list");


DailyMovie movie = new DailyMovie();
for (int a = 0; a < jsonArray.length(); a++) {
JSONObject obj = jsonArray.getJSONObject(a);
System.out.println("Object Value " + a + " " + obj.toString());

movie.setId(obj.getString("id"));

movie.setTitle(obj.getString("title"));

movie.setOwner(obj.getString("owner"));

movie.setChannel(obj.getString("channel"));
// adding movie to movies array

movieList.add(movie);
}


} catch (JSONException e) {
e.printStackTrace();
}


// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener()

{
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error");
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();

}
});

// Adding request to request queue
app.AppController.getInstance().addToRequestQueue(movieReq);
}

@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}

private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


and here is DailyMovie class

package com.example.ankurdell.customlistview;

/**
* Created by Ankur Dell on 8/20/2017.
*/

public class DailyMovie {
private String id;
private String title;
private String channel;
private String owner;

public DailyMovie() {

}

public DailyMovie(String moviename, String movieid, String theme, String movieowner) {
this.id = movieid;
this.title = moviename;
this.channel = theme;
this.owner = movieowner;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getChannel() {
return channel;
}

public void setChannel(String channel) {
this.channel = channel;
}

public String getOwner() {
return owner;
}

public void setOwner(String owner) {
this.owner = owner;
}
}


can some one advice where I m doing wrong. I referred https://www.androidhive.info/2014/09/android-json-parsing-using-volley/ to do the coding.

Answer Source

DailyMovie is just only one element. right?

You have to declare DailyMovie variable in for statement

for (int a = 0; a < jsonArray.length(); a++) {
 JSONObject obj = jsonArray.getJSONObject(a);
 System.out.println("Object Value " + a + " " + obj.toString());

 DailyMovie movie = new DailyMovie();

 movie.setId(obj.getString("id"));                        
 movie.setTitle(obj.getString("title"));                      
 movie.setOwner(obj.getString("owner"));                                  
 movie.setChannel(obj.getString("channel"));

 // adding movie to movies array
 movieList.add(movie);

}