Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么尽管获得了正确的数据,RecyclerView适配器仍不能正确更新(仅第一次更新)?_Java_Android_Listview_Android Fragments_Android Recyclerview - Fatal编程技术网

Java 为什么尽管获得了正确的数据,RecyclerView适配器仍不能正确更新(仅第一次更新)?

Java 为什么尽管获得了正确的数据,RecyclerView适配器仍不能正确更新(仅第一次更新)?,java,android,listview,android-fragments,android-recyclerview,Java,Android,Listview,Android Fragments,Android Recyclerview,这很奇怪。出于某些原因,尽管从REST调用中获得了正确的数据,但在RecyclerView中适配器没有正确更新 以下是两种方法: public Observable<Response<GetTopicsByCreatorResponseBody>> getTopicsByCreator(GetTopicsByCreatorRequest request) { return getApi() .getTopics(request)

这很奇怪。出于某些原因,尽管从REST调用中获得了正确的数据,但在RecyclerView中适配器没有正确更新

以下是两种方法:

public Observable<Response<GetTopicsByCreatorResponseBody>> getTopicsByCreator(GetTopicsByCreatorRequest request) {
    return getApi()
            .getTopics(request)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
}

public void getTopicsByCreator(GetTopicsByCreatorRequest request) {
    mBinding.swipeRefreshContainer.setRefreshing(true);
    Log.d(TAG, "getTopicsByCreator: in request" + request.getUserId());
    Disposable disposable = mViewModel.getTopicsByCreator(request)
            .subscribe((Response<GetTopicsByCreatorResponseBody> response) -> {
                if (response.body() != null) {
                    GetTopicsByCreatorResponseBody body = response.body();
                    if (body.getTopics() != null) {
                        Log.d(TAG, "getTopicsByCreator: inside" + body.getTopics().get(0).getCreator().getUserId());
                        List<Topic> topics = body.getTopics();
                        Log.d(TAG, "getTopicsByCreator: " + topics.size());
                        RecyclerView.Adapter adapter = mBinding.recyclerForumTopicsForUser.getAdapter();
                        if (adapter instanceof GetTopicsByCreatorAdapter) {
                            ((GetTopicsByCreatorAdapter) adapter).setTopics(topics);
                            Log.d(TAG, "getTopicsByCreator: instanceof" + ((GetTopicsByCreatorAdapter) adapter).getItemCount());
                        } else {
                            adapter = GetTopicsByCreatorAdapter.getInstance(topics);
                            Log.d(TAG, "getTopicsByCreator: new instance" + ((GetTopicsByCreatorAdapter) adapter).getItemCount());
                            mBinding.recyclerForumTopicsForUser.setAdapter(adapter);
                        }
                    }
                }
            }, (Throwable ex) -> {
                Log.e(TAG, "getTopicsByCreator: " + ex.getClass().getSimpleName() + ": " + ex.getMessage());
            });
    mViewModel.addDisposable(disposable);
    mBinding.swipeRefreshContainer.setRefreshing(false);
}
getInstance(主题)代码如下所示:

public static GetForumTopicsByCreatorAdapter getInstance(List<Topic> topics) {
    if (sInstance == null) {
        synchronized (GetForumTopicsByCreatorAdapter.class) {
            if (sInstance == null) {
                sInstance = new GetForumTopicsByCreatorAdapter(topics);
                sInstance.notifyDataSetChanged();
            } else {
                sInstance.setTopics(topics);
            }
        }
    }
    return sInstance;
}

public void setTopics(List<Topic> topics) {
    mTopics = topics;
    notifyDataSetChanged();
}
公共静态GetForumTopicsByCreator适配器getInstance(列出主题){
if(sInstance==null){
已同步(GetForumTopicsByCreatorAdapter.class){
if(sInstance==null){
sInstance=新的GetForumTopicsByCreator适配器(主题);
sInstance.notifyDataSetChanged();
}否则{
sInstance.setTopics(主题);
}
}
}
回归承诺;
}
公共主题(列出主题){
mTopics=主题;
notifyDataSetChanged();
}
公共静态GetForumTopicsByCreator适配器getInstance(列出主题){
//两次检查是否为空
如果(sInstance==null){//1
已同步(GetForumTopicsByCreatorAdapter.class){
如果(sInstance==null){//这里是一个
sInstance=新的GetForumTopicsByCreator适配器(主题);
sInstance.notifyDataSetChanged();
}否则{
sInstance.setTopics(主题);
}
}
}
///此时,若实例不为null,则不会对其执行任何操作并按原样返回。
///因此,不会添加新项目。
回归承诺;
}
此代码不正确。看看它,想想当实例不为null时会发生什么

public static GetForumTopicsByCreatorAdapter getInstance(List<Topic> topics) {

    // this is what heppens when instance is not null. topics variable is not used
    return sInstance;
}
公共静态GetForumTopicsByCreator适配器getInstance(列出主题){
//当实例不为空时,这就是heppens所做的。不使用topics变量
回归承诺;
}
public static GetForumTopicsByCreatorAdapter getInstance(List<Topic> topics) {

   // two checks for null 
    if (sInstance == null) { //one here
        synchronized (GetForumTopicsByCreatorAdapter.class) {
            if (sInstance == null) {  //and one here
                sInstance = new GetForumTopicsByCreatorAdapter(topics);
                sInstance.notifyDataSetChanged();
            }  else  {
                sInstance.setTopics(topics);
            }
        }
    }
    /// at this point, if instance was not null, nothing is done on it and is returned as is.
     /// So new items are not added.
    return sInstance;
}
public static GetForumTopicsByCreatorAdapter getInstance(List<Topic> topics) {

    // this is what heppens when instance is not null. topics variable is not used
    return sInstance;
}