I am a novice in android. I am learning data parsing, using Json. I am storing data to a arraylist. The problem is, sometime the arraylist shows all the data and most of the times it seems empty. I set a toast message for all iterator and in toast message all data are showing always, in fact while list is showing empty, toast is showing data, It means data parsing is alright, problem is occurring at the time of adding them into list. Why I am not getting the data in the list all the time and how can I get rid of it.Thanks in advance.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.list);
list = new ArrayList<String>();
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
RequestQueue requestQueue = VolleySingleTon.getInstance().getRequestQueue();
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET
, url
, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// list = worksonresponse(response);
try {
JSONArray arraymovie = response.getJSONArray("movies");
for (int i = 0; i < 20; i++) {
JSONObject currentmovie = arraymovie.getJSONObject(i);
// String name = currentmovie.getString("title");
list.add(arraymovie.getJSONObject(i).getString("title"));
makeToast(arraymovie.getJSONObject(i).getString("title"));
lv.setAdapter(adapter);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
makeerror(error);
}
});
requestQueue.add(objectRequest);
// requestQueue.add(stringRequest);
//adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
// lv.setAdapter(adapter);
if (list == null || list.size() == 0) {
Log.e("about list", "list is empty");
} else {
Log.e("about list", "size is =" + list.size());
}
}
Put this line
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
right after this line:
list = new ArrayList<String
and add
adapter.notifyDatasetChanged()
after your for
loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.