Invalid solve the problem using adapter.notifyDataSetChanged () after RecyclerView to obtain data from the network

I am using Retrofit framework, because I use asynchronous usage Retrofit, so after obtaining data from the network to the need to call adapter.notifyDataSetChanged () method to update the data recyclerView, but here it is easy to swap pit: recyclerView data set productList is not the same (as to why not, I do not know ......), as shown:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description

Know the crux of the problem, we can prescribe the right medicine, a new method to refresh the data in the adapter

    public void updateData(List<Product> list) {// 更新数据方法
        productList.clear();
        productList.addAll(list);
        getProductRcvItemList();
        notifyDataSetChanged();
    }
// 从网络中获取商品的信息
    public void getProductsInfoFromNet(String keyword, Integer categroryId, int pageNum, int PageSize, String orderBy) {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.baseProductPath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface.getProductsList2 requestGetProductsList = retrofit.create(RequestInterface.getProductsList2.class);
        Call<ServerResponse<MyPageInfo<Product>>> call = requestGetProductsList.getProducts(keyword, categroryId, pageNum, PageSize, orderBy);

        call.enqueue(new Callback<ServerResponse<MyPageInfo<Product>>>() {
            @Override
            public void onResponse(Call<ServerResponse<MyPageInfo<Product>>> call, Response<ServerResponse<MyPageInfo<Product>>> response) {
                if (response.body() == null) {
                    new MainActivity().toastShort("网络异常,请检查网络配置");
                    return;
                } else {
                    MyPageInfo<Product> myPageInfo = response.body().getData();// 获取页面信息
                    List<Product> productList = myPageInfo.getList();// 获取页面信息中的商品信息
                    
                    **adapter.updateData(productList);// 更新数据**
                }
            }

            @Override
            public void onFailure(Call<ServerResponse<MyPageInfo<Product>>> call, Throwable t) {
                new MainActivity().log("onFailure");
            }
        });
    }

The first clear out the list, and then re-add, ok, you're done!

We hope to help children's shoes to the pit

Published 19 original articles · won praise 5 · Views 4203

Guess you like

Origin blog.csdn.net/qq_41409120/article/details/85345187