RecyclerView / Adapter不会刷新Asyntask中的新数据

时间:2016-11-23 10:04:47

标签: android android-asynctask android-recyclerview android-adapter recycler-adapter

在主要活动中,我将API称为下载最受欢迎的电影。下载电影时,我会调用adatper .notifyDataSetChanged(); 此调用在Asyntask的onPostExecute()中进行,但对回收器没有任何更改。

主要部分:

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();

这是我调用加载电影并通知适配器的方法。

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();
    }

对API的请求是正确的,我得到的信息,因为当我调试时,我可以看到它。

我怎样才能刷新?

更新

enter image description here

2 个答案:

答案 0 :(得分:0)

你能否展示你的适配器类。我认为你必须在适配器类中创建setmovies()函数,然后在postexecute()

之前在mMovieAdapterRecyclerView.notifyDataSetChanged();方法中调用该函数

答案 1 :(得分:-2)

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

检查列表是否正在获得正确的电影列表