I searched everywhere, trying to figure out why my code is causing the problem. I have a GridView that has an ArrayAdapter that takes photos using AsyncTask. I can see the elements being updated, but when I try to update the adapter, the GridView does not seem to update the new view.
This is the appropriate code that does the work ...
private void fetchJsonResponse(String url) { // Pass second argument as "null" for GET requests JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url + "&api_key=" + API_KEY, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { JSONArray photos = response.getJSONArray("photos"); for(int i = 0; i < photos.length(); i++){ JSONObject object = photos.getJSONObject(i); String url = object.getString("img_src"); //String id = object.getString("id"); list.add(new ImageItem(null, "Picture", url)); Log.i("Debug 2", url); } Log.i("Debug 2", list.get(0).toString()); if(gridViewAdapter != null){ gridViewAdapter.clear(); gridViewAdapter.addAll(list); gridViewAdapter.notifyDataSetChanged(); gridView.invalidateViews(); } else { gridViewAdapter = new GridViewAdapter(getActivity(), R.layout.gridview_item, list); gridView.setAdapter(gridViewAdapter); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.e("Error: ", error.getMessage()); } }); /* Add your Requests to the RequestQueue to execute */ mRequestQueue.add(req); } private class MyAsyncTask extends AsyncTask<String, Void, Void> { private ProgressDialog progressDialog; private Context context; public MyAsyncTask (Context context){ this.context = context; progressDialog = new ProgressDialog(getActivity()); progressDialog.setMessage("Contacting Rover..."); } @Override protected Void doInBackground(String... strings) { fetchJsonResponse(strings[0]); return null; } @Override protected void onPreExecute() { super.onPreExecute(); Toast.makeText(getActivity(), "In Pre Execute", Toast.LENGTH_SHORT).show(); progressDialog.show(); } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); progressDialog.dismiss(); } }
I would really appreciate any help if possible. Trying to get the application before the new years :).
Maybe if you could tell me why this is happening, then it will not cause a problem, while others will see.
EDIT: Added a bit more code, which is updated after clicking the button twice.
private void fetchJsonResponse(String url) { // Pass second argument as "null" for GET requests JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url + "&api_key=" + API_KEY, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { JSONArray photos = response.getJSONArray("photos"); list.clear(); for(int i = 0; i < photos.length(); i++){ JSONObject object = photos.getJSONObject(i); String url = object.getString("img_src"); list.add(new ImageItem(null, "Picture", url)); Log.i("Debug 2", url); } Log.i("Debug 2", list.get(0).toString()); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.e("Error: ", error.getMessage()); } }); /* Add your Requests to the RequestQueue to execute */ mRequestQueue.add(req); } private class MyAsyncTask extends AsyncTask<String, Void, Void> { private ProgressDialog progressDialog; private Context context; public MyAsyncTask (Context context){ this.context = context; progressDialog = new ProgressDialog(getActivity()); progressDialog.setMessage("Contacting Rover..."); pictureAdapter = new PictureAdapter(getActivity(), list); gridView.setAdapter(pictureAdapter); } @Override protected Void doInBackground(String... strings) { fetchJsonResponse(strings[0]); return null; } @Override protected void onPreExecute() { progressDialog.show(); super.onPreExecute(); Toast.makeText(getActivity(), "In Pre Execute", Toast.LENGTH_SHORT).show(); } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); pictureAdapter.updateItemList(list); gridView.invalidate(); progressDialog.dismiss(); } }
Adapter:
public class PictureAdapter extends BaseAdapter { private ArrayList<ImageItem> items; private Context context; private TextView titleText; private ImageView itemImage; public PictureAdapter(Context context, ArrayList<ImageItem> items){ this.context = context; this.items = items; } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = LayoutInflater.from(context).inflate(R.layout.gridview_item, parent, false); titleText = (TextView) v.findViewById(R.id.text); itemImage = (ImageView)v.findViewById(R.id.image); titleText.setText(items.get(position).getTitle()); Picasso.with(context).load(items.get(position).getUrl()).fit().into(itemImage); return v; } public void updateItemList(ArrayList<ImageItem> newItemList){ this.items = newItemList; notifyDataSetChanged(); } }