1

I am able to show images (set by default) in the gridview properly using a custom adapter. However when I try to change the string[] in ForecastFragment (snippets below), the updateList function is not getting called. I would like the all the views in the grid to be recreated.

ImageAdapter.java :

public class ImageAdapter extends BaseAdapter {
public Context mContext;   ImageAdapter(Context c) {
    mContext = c;
    String LOG_TAG = ImageAdapter.class.getSimpleName();
    Log.e(LOG_TAG,"Constructor Context  :  "+mContext);


}

public int getCount() {
    return this.eatFoodyImages.length;
}
public int size1;
public int size2;
public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

public void updateList(String[] string)
{
    this.eatFoodyImages=string;
    this.notifyDataSetChanged();
    String LOG_TAG = ImageAdapter.class.getSimpleName();
    Log.e(LOG_TAG, "\n\nupdatelist  :  " + this.eatFoodyImages);
}


// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
       size1 = (int) this.mContext.getResources().getDimension(R.dimen.image_size1);
       size2 = (int) this.mContext.getResources().getDimension(R.dimen.image_size2);

        imageView.setLayoutParams(new GridView.LayoutParams(size1,size2));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(0, 0, 0, 0);

    } else {
        imageView = (ImageView) convertView;
    }
    String LOG_TAG = ImageAdapter.class.getSimpleName();
    Log.e(LOG_TAG, "\n\n\nPosition :  " + position + "   Strings Array : \n\n" + eatFoodyImages[position].toString());
    //System.out.println();

        Picasso.with(mContext)
                .

        load(eatFoodyImages[position])

                .

                into(imageView);


        return imageView;
    }


            // references to our images
    public String[] eatFoodyImages = {
        "http://i.imgur.com/rFLNqWI.jpg",               //Default Random Data!!!
        "http://i.imgur.com/C9pBVt7.jpg",
        "http://i.imgur.com/rT5vXE1.jpg",
        "http://i.imgur.com/aIy5R2k.jpg",
        "http://i.imgur.com/MoJs9pT.jpg",
        "http://i.imgur.com/S963yEM.jpg",
        "http://i.imgur.com/rLR2cyc.jpg",
        "http://i.imgur.com/SEPdUIx.jpg",
        "http://i.imgur.com/aC9OjaM.jpg",
        "http://i.imgur.com/76Jfv9b.jpg",
        "http://i.imgur.com/fUX7EIB.jpg",
        "http://i.imgur.com/syELajx.jpg",
        "http://i.imgur.com/COzBnru.jpg",
        "http://i.imgur.com/Z3QjilA.jpg",
  };
 }

ForecastFragment.java :

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

       View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    GridView gridview = (GridView) rootView.findViewById(R.id.gridview);

    movieadapter = new ImageAdapter(this.getActivity());
    gridview.setAdapter(movieadapter);

    });

    return rootView;
}

Calling updateList :

 public class FetchWeatherTask extends AsyncTask<String, Void, String[]>
{//Showing only relevant code 

IA.updateList(eatFoodyImages); //Here eatFoodyImages contains the new String[] to be used for image loading.

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
Siddharth Sharma
  • 182
  • 1
  • 1
  • 10

1 Answers1

0

Here it goes, i have a adapter like below

public View getView(int position, View convertView, ViewGroup parent) {

        Movie movie = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.movie_tile, parent, false);
        }

        ImageView imageView = (ImageView) convertView.findViewById(R.id.movie_image);
        Picasso.with(this.getContext()).load(Constants.MOVIE_POSTER_PATH_SMALL + movie.getPosterPath()).placeholder(ContextCompat.getDrawable(this.getContext(), R.drawable.movie)).error(ContextCompat.getDrawable(this.getContext(), R.drawable.movie)).into(imageView);


        return convertView;
    }

which sets a tile in a gridview.

I trigger a asynctask and get the movies from API then notify the dataset which loads the movies and refreshes the view.

So essentially your notifyDataSet() should be in onPostExecute() of AsyncTask.

Here is my Async task code for better understanding.

public class CallMovieDBAPI extends AsyncTask<String, Void, List<Movie>> {

        ProgressDialog dialog;
        private Activity activity;
        private GridView gridView;

        public CallMovieDBAPI(Activity activity, GridView gridView) {
            this.activity = activity;
            this.gridView = gridView;
        }

        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(getActivity());
            dialog.setMessage(activity.getString(R.string.progressDialogText));
            dialog.show();
        }

        @Override
        protected List<Movie> doInBackground(String... params) {
            InputStream in = null;
            String jsonResponse = null;
            String apiURL = params[0];
            try {

                URL url = new URL(apiURL);

                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                in = new BufferedInputStream(urlConnection.getInputStream());
                jsonResponse = getJsonObject(in);
                movieList = JsonParser.parse(jsonResponse);

            } catch (Exception e) {

                Log.e("CallMovieDBAPI", "Exception occurred in AsyncTask - CallMovieDBAPI", e);
            }

            return movieList;


        }

        @Override
        protected void onPostExecute(List<Movie> movies) {
            Log.d("Popular Movies", "Got data successfully");
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            movieAdapter = new MovieAdapter(activity, movieList);
            gridView.setAdapter(movieAdapter);
            movieAdapter.notifyDataSetChanged();
            if (isTablet) {
                ((MovieClickedCallback) getActivity()).onMovieClicked(0, movieAdapter);
            }

        }

Let me know if this solves.

Aravind
  • 82
  • 1
  • 8