notifyDatasetChanged()无法在RecyclerView上运行

时间:2015-08-24 13:13:18

标签: android android-recyclerview recycler-adapter

我想显示在回收站视图中提取的帖子列表,但回收站视图未显示任何内容。我使用ButterKnife库绑定视图和android asyncHttp库来获取数据。

当我使用相同的代码而不使用任何这些库时,代码可以正常工作(使用异步任务)

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_post_list, container, false);
        ButterKnife.bind(this, mRootView);

        //Initialize
        init();

        //Set progress view
        setProgress();

        //Get post list
        get();
        return mRootView;
    }

private void init() {
        mContentLoader.setVisibility(View.GONE);
        mPostRecyclerView = (RecyclerView) mRootView.findViewById(R.id.mPostRecyclerView);

        final LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mLayoutManager.scrollToPosition(0);
        mPostRecyclerView.setLayoutManager(mLayoutManager);

        mPostArrayList = new ArrayList<>();
        mPostRecyclerAdapter = new PostRecyclerAdapter(getActivity(), mPostArrayList);
        mPostRecyclerView.setAdapter(mPostRecyclerAdapter);

    }

private void get() {
        //Request parameters
        RequestParams params = new RequestParams();
        params.put("key", API_KEY);
        params.put("prettyPrint", "false");
        params.put("view", "reader");
        params.put("fetchImages", "true");
        params.put("fields", "items(author/displayName,content,images,labels,replies(selfLink,totalItems),title,titleLink,updated,url),nextPageToken");

        BloggerRESTClient.get("/posts", params, new JsonHttpResponseHandler() {

            @Override
            public void onProgress(long bytesWritten, long totalSize) {
                super.onProgress(bytesWritten, totalSize);
                mLoaderProgress.setText((long) 100 * bytesWritten / totalSize + "% Loading Posts");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try {
                    //Get and update nextPageToken
                    if (response.has("nextPageToken"))
                        mNextPageToken = response.getString("nextPageToken");

                    //Get posts
                    if (response.has("items")) {
                        JSONArray itemsArray = response.getJSONArray("items");
                        for (int i = 0; i < itemsArray.length(); i++) {
                            JSONObject itemsObject = itemsArray.getJSONObject(i);
                            Posts posts = new Posts(itemsObject);
                            mPostArrayList.add(i, posts);
                        }
                        Log.d("size", "" + mPostArrayList.size());
                        mPostRecyclerAdapter.notifyDataSetChanged();
                        setSlider();
                        mProgressView.setVisibility(View.GONE);
                        mContentLoader.setVisibility(View.VISIBLE);
                    } else {
                        Toast.makeText(getActivity(), "No Posts to Show!", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);

                //Show error status on failure
                Toast.makeText(getActivity(), "Error: HTTP " + statusCode, Toast.LENGTH_LONG).show();
            }
        });
    }

2 个答案:

答案 0 :(得分:0)

首先将您的项目添加到mPostArrayList,然后使用:

mAdapter.notifyItemInserted(mPostArrayList.size()-1)

这比使用

更好
mAdapter.notifyDataChanged()

表现。

答案 1 :(得分:0)

这似乎是一个逻辑错误:它将取代原始的帖子项目。

JSONArray itemsArray = response.getJSONArray("items");
                        for (int i = 0; i < itemsArray.length(); i++) {
                            JSONObject itemsObject = itemsArray.getJSONObject(i);
                            Posts posts = new Posts(itemsObject);
                            mPostArrayList.add(i, posts);
                        }

将其更改为:

 JSONArray itemsArray = response.getJSONArray("items");
for(JSONObject itemsObject : itemsArray ) {
      Posts posts = new Posts(itemsObject);
      mPostArrayList.add(posts);
}

其余代码似乎很好。