Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView not updating its child views after adapter gets new items

I've been looking around everywhere in trying to find out why my code was causing an issue. I have a GridView that has an ArrayAdapter which pulls photos down with an AsyncTask. I can see the items being updated but when I try to update the adapter the GridView doesn't seem to update with the new view.

This is the relevant 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 app out before new years :).

Maybe If you could tell me why this happens so It won't cause an issue again and other will see.

EDIT: Added a bit more code which has it refreshing after I click 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();
}

}
like image 218
SmiffyKmc Avatar asked Oct 29 '22 15:10

SmiffyKmc


1 Answers

Try the below lines in post execute

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);

    pictureAdapter.updateItemList(list,gridView);
    progressDialog.dismiss();
}

Now in your updateItemList

public void updateItemList(ArrayList<ImageItem> newItemList,GridView gridView){
    this.items = newItemList;

    gridView.setAdapter(null);
    gridView.invalidateViews();
    gridView.deferNotifyDataSetChanged();

    gridView.setAdapter(list);

}
like image 103
Ranjan Avatar answered Nov 15 '22 07:11

Ranjan