Gridview not updated (Android)

Asked

Viewed 47 times

0

I am creating an app that when starting it performs an asynchronous task that queries an api, when picking the result it will insert into Adapter. I’ve tried several ways, but only the images appear when I select from the Sort by menu and run the task again.

Whenever run appears on logcat: E/GED: Failed to get GED Log Buf, err(0)

I would like the images to be uploaded to Gridview as soon as they are executed.

Follows my code:

public class BrowserMoviesActivityFragment extends Fragment {

private MovieAdapter m_MovieAdapter;
private List<Movie> m_Movies;
private ProgressDialog m_ProgressDialog;
private String m_SortType = Constants.SORT_POPULAR_PARAM;
private MenuItem m_MenuItemSortPopular;
private MenuItem m_MenuItemSortRating;

public BrowserMoviesActivityFragment() { setHasOptionsMenu(true); }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_browser_movies, container, false);

    m_Movies = new ArrayList<>();
    m_MovieAdapter = new MovieAdapter(getActivity(), m_Movies);

    GridView gridViewMovie = (GridView) rootView.findViewById(R.id.gridViewMovie);
    gridViewMovie.setAdapter(m_MovieAdapter);

    gridViewMovie.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getActivity(), DetailMovieActivity.class).putExtra("MOVIE", m_Movies.get(position));
            startActivity(intent);
        }
    });

    if(isOnline()) {
        new FetchMoviesTask().execute(m_SortType);
    }

    return rootView;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_fragment_browser_movies, menu);

    m_MenuItemSortPopular = menu.findItem(R.id.action_sort_popular);
    m_MenuItemSortRating = menu.findItem(R.id.action_sort_rating);

    if(m_SortType.contentEquals(Constants.SORT_POPULAR_PARAM)) {
        if(!m_MenuItemSortPopular.isChecked()) {
            m_MenuItemSortPopular.setChecked(true);
        }
    } else if(m_SortType.contentEquals(Constants.SORT_RATE_PARAM)) {
        if(!m_MenuItemSortRating.isChecked()) {
            m_MenuItemSortRating.setChecked(true);
        }
    }

}

@Override
public  boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch(item.getItemId()) {
        case R.id.action_sort_popular:
            if(isOnline()) {
                changeSort(Constants.SORT_POPULAR_PARAM);
                if(!m_MenuItemSortPopular.isChecked()) {
                    m_MenuItemSortPopular.setChecked(true);
                }
            }
            return true;
        case R.id.action_sort_rating:
            if (isOnline()) {
                changeSort(Constants.SORT_RATE_PARAM);
                if(!m_MenuItemSortRating.isChecked()) {
                    m_MenuItemSortRating.setChecked(true);
                }
            }
            return true;
        default:
            return true;
    }
}

private void changeSort(String sort) {
    m_SortType = sort;
    if(isOnline()) {
        new FetchMoviesTask().execute(m_SortType);
    }
}

public boolean isOnline() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if(networkInfo == null || !networkInfo.isConnected() || !networkInfo.isAvailable()) {
        Toast.makeText(getContext(), getString(R.string.message_offline), Toast.LENGTH_SHORT).show();
        return false;
    }
    return true;
}

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

    IMovieService m_MovieService;
    List<Movie> movieList = null;

    @Override
    protected void onPreExecute() {
        m_ProgressDialog = ProgressDialog.show(getContext(), "Loading...", "Searching movies...");
    }

    @Override
    protected List<Movie> doInBackground(String... params) {
        String sort = params[0];
        m_MovieService = MovieClient.createService(IMovieService.class);

        Call<ResponseAPI<Movie>> moviesCall = m_MovieService.getMovies(sort);
        try {
            Response<ResponseAPI<Movie>> response = moviesCall.execute();
            movieList = response.body().getResults();
        } catch (Exception e) {
            Toast.makeText(getContext(), getString(R.string.message_offline), Toast.LENGTH_SHORT).show();
        }
        return movieList;
    }

    @Override
    protected void onPostExecute(List<Movie> result) {
        m_MovieAdapter.clear();
        if(result != null) {
            for (Movie movie : result) {
                m_MovieAdapter.add(movie);
            }
        }
        m_ProgressDialog.dismiss();
    }
}

}

  • When you modify the Adapter data try calling m_MoviewAdapter.notifyDataSetChanged()

  • Oops, I’ve done this before, but it’s still the same.

1 answer

0

This is happening because you are modifying the Adapter data, but you are not "warning" it. For this just call the method m_MoviewAdapter.notifyDataSetChanged() after adding or removing a movie

Modify the method onPostExecute() of your FetchMoviesTask for :

@Override
    protected void onPostExecute(List<Movie> result) {
        m_MovieAdapter.clear();
        if(result != null) {
            for (Movie movie : result) {
                m_MovieAdapter.add(movie);
            }

         m_MoviewAdapter.notifyDataSetChanged();
        }
        m_ProgressDialog.dismiss();
    }
  • Oops, I’ve done it, but it’s still the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.