Jordi Jordi - 1 month ago 13
JSON Question

JsonObjectRequest shows empty ListView from JSON

Good afternoon,

I'm trying to use a Singleton for retrieve and populate a ListView for an Android App using Android Studio with the latest version of Volley and I'm having problem when I have to show the information.

The JSON I get from my server is fine, and when I create a product everything seems fine, but the ListView is not populated.

Can you show me some light? I have tried a lot of tutorials and examples and I don't know my ListView is still empty, because I have checked that the products are created in my function.

Here you are my code:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListadoProductos extends Activity {

// URL of object to be parsed
String url = "https://www.mywebsite.com/products.json";

// Key JSON
String key = "lista_productos";

// Map
List<Map<String,String>> productoList = new ArrayList<Map<String,String>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.products);

// Get the latest products
initJSON();

// Show the information
ListView listView = (ListView) findViewById(R.id.listv);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, productoList, android.R.layout.simple_list_item_1, new String[] {key}, new int[] {android.R.id.text1});
listView.setAdapter(simpleAdapter);
}

private HashMap<String, String> createProducto(String nameTitle, String name){
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(nameTitle, name);
return employeeNameNo;
}

private void initJSON(){

JsonObjectRequest jsObjRequest = new JsonObjectRequest (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
try{
JSONObject jsonResponse = new JSONObject(response.toString());
JSONArray jsonMainNode = jsonResponse.optJSONArray(key);

// Show 10 products
for(int i = 0; i<10;i++){
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.getString("nombre");
String outPut = name;
productoList.add(createProducto(key, outPut));
}
}
catch(JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.d("Error", error.toString());
}
});

// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
}
}


Much appreciated.

Answer

the server call is not in MainThread so when you initialize simpleAdapter the productoList is probably empty,you have to initialize the adapter in onResponce method so just simply move

SimpleAdapter simpleAdapter = new SimpleAdapter(this, productoList, android.R.layout.simple_list_item_1, new String[] {key}, new int[] {android.R.id.text1});
listView.setAdapter(simpleAdapter);

to your onResponse(JSONObject response) method

Comments