-3

In the main activity I call a API to downlaod most popular movies. When movies are downloaded I call the adatper .notifyDataSetChanged(); This call is made in onPostExecute()of Asyntask, but there is no changes on recycler.

Part of Main:

mMovieList = new ArrayList<>();
        mMovieAdapterRecyclerView = new MovieAdapterRecyclerView(this, mMovieList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mMovieAdapterRecyclerView);
        receiveMovies();

Here is the method I call to load movies and notify the adapter.

private void receiveMovies() {

        new AsyncTask<Void, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(Void... voids) {
                try {
                    JSONObject response = null;
                    OkHttpClient client = new OkHttpClient();
                    MediaType JSON = MediaType.parse("application/json; charset=utf-8");

                    String mUrl = ConnectionUtils.TRAKT_URL + ConnectionUtils.POPULAR + ConnectionUtils.ALL_INFO;
                    Request request = new Request.Builder()
                            .url(mUrl)
                            .addHeader(ConnectionUtils.TRAKT_API_VERSION, ConnectionUtils.TRAKT_API_VERSION_NUM)
                            .addHeader(ConnectionUtils.TRAKT_API_KEY, ConnectionUtils.API_KEY)
                            .addHeader(ConnectionUtils.TRAKT_CONTENT, JSON.toString())
                            .get()
                            .build();

                    Response clientResponse = client.newCall(request).execute();
                    int code = clientResponse.code();
                    String responseJson = clientResponse.body().string();
                    Gson gson = new Gson();
                    Type listType = new TypeToken<List<Movie>>() {
                    }.getType();
                    mMovieList = gson.fromJson(responseJson, listType);
                    return true;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return false;
            }

            @Override
            protected void onPostExecute(Boolean aBoolean) {
                if(aBoolean) {
                    mMovieAdapterRecyclerView.notifyDataSetChanged();
                }
            }
        }.execute();
    }

The request to the API is correct and I get the information, because when I debug, I can see it.

How can I made it refresh?

UPDATE

enter image description here

Shudy
  • 7,806
  • 19
  • 63
  • 98
  • You do not need an AsyncTask when you use OkHttp... http://stackoverflow.com/questions/34967505/android-okhttp-asynchronous-calls – OneCricketeer Nov 23 '16 at 10:07
  • 1
    Please learn java's basic ... assigning values to variable .... – Selvin Nov 23 '16 at 10:07
  • instead of `mMovieList = gson.fromJson(responseJson, listType);` could you try `mMovieList.addAll(gson.fromJson(responseJson, listType))` – Sanif SS Nov 23 '16 at 10:08
  • @SanifSS it would be better to clear it first ... but yeah ... that's the problem :) – Selvin Nov 23 '16 at 10:09
  • http://ideone.com/TOsxz3 – Selvin Nov 23 '16 at 10:14
  • 1
    See here. Note the comment that says *do not*. https://github.com/codepath/android_guides/wiki/Using-the-RecyclerView#notifying-the-adapter – OneCricketeer Nov 23 '16 at 10:15
  • @Selvin maybe I'm not expert in Java, neither in Android, but yes.. i know how to assignin values to variables., I update my post, with image, where you can see that mMovieList is filled with the information. Another thing is I don't know how really works the recycler. Thats another thing. – Shudy Nov 23 '16 at 10:18
  • If you don't understand RecyclerView, there's really no clear advantage over implementing it over a ListView. That isn't the problem, though. The adapter still shouldn't have its underlying list object reassigned. – OneCricketeer Nov 23 '16 at 10:20
  • it seems like you still dont get it ... http://ideone.com/TOsxz3 <= you are expecting to get bbbbb in this example – Selvin Nov 23 '16 at 10:20
  • @cricket_007 Thanks , I'm taking a look to the link you provide. And If I dont use Recyler I'll never undestand it. thats why I'm testing it. Thanks! – Shudy Nov 23 '16 at 10:24
  • 1
    Again, thanks @cricket_007 I solved with adding the objects that are given by gson, to mMovieList – Shudy Nov 23 '16 at 10:36

2 Answers2

0

Can you please show your adapter class. I think you have to make setmovies() function in adapter class and then call that function in postexecute() method before mMovieAdapterRecyclerView.notifyDataSetChanged();

Sarbjyot
  • 136
  • 2
  • 8
-2
 if(aBoolean) {
               // pass the updated list here which you get after downloading
                // like mMovieAdapterRecyclerView.updateMoviewList(passList)
                    mMovieAdapterRecyclerView.notifyDataSetChanged();
                }

check whether list is getting proper movie list or not

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • How would you pass the boolean and the list? This isn't necessary, though. The list can directly be accessed by the member variable – OneCricketeer Nov 23 '16 at 10:16
  • List is Global so we can pass reference to adapter and pass boolean value which is getting inside onPostExecute – Jitesh Mohite Nov 23 '16 at 10:19