recyclerview 上没有显示数据

No Data displayed on recyclerview

我正在学习mvvm结构,我用mvvm结构做了一个应用。我也用过 room 和 RxJava

代码

public class ScoresActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    List<ScoreDataViewModel> scoreDataViewModelList;
    ScoreAdapter scoreAdapter;
    ScoreViewModel scoreViewModel;
    private final CompositeDisposable mDisposable = new CompositeDisposable();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);

        init();
    }

    private void init() {
        recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        scoreDataViewModelList=new ArrayList<>();
        scoreAdapter=new ScoreAdapter(scoreDataViewModelList);
        recyclerView.setAdapter(scoreAdapter);

        ScoreViewModelFactory scoreViewModelFactory = Injection.provideScoreViewModelFactory(this);
        scoreViewModel= ViewModelProviders.of(this,scoreViewModelFactory).get(ScoreViewModel.class);

    }

    @Override
    protected void onStart() {
        super.onStart();
        mDisposable.add(scoreViewModel.getScoreDataViewModels()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<ScoreDataViewModel>>() {
                    @Override
                    public void accept(List<ScoreDataViewModel> scoreDataViewModels) throws Exception {
                        scoreDataViewModelList=scoreDataViewModels;
                        System.out.println(scoreDataViewModelList.toString());
                        Log.e("size", String.valueOf(scoreDataViewModelList));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.e("Unable to update list", throwable.toString());
                    }
                }));
    }
}

在这里,我可以看到日志中打印的尺寸是正确的。 但是我在这里看不到项目。

Activity得分

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sevenbits.android.mvvmsample.view.ScoresActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent">
    </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

现在,ScoreViewModel

public class ScoreViewModel extends ViewModel {

    private UserDataSource userDataSource;
    List<ScoreDataViewModel> scoreDataViewModelList;

    public ScoreViewModel(UserDataSource userDataSource) {
        this.userDataSource = userDataSource;
    }

    public Flowable<List<ScoreDataViewModel>> getScoreDataViewModels() {

        return userDataSource.getUsers().map(new Function<List<User>, List<ScoreDataViewModel>>() {
            @Override
            public List<ScoreDataViewModel> apply(List<User> users) throws Exception {

                List<ScoreDataViewModel> scoreDataViewModels = new ArrayList<ScoreDataViewModel>();
                for (User user : users) {
                    scoreDataViewModels.add(new ScoreDataViewModel(user));
                }

                return scoreDataViewModels;
            }
        });
    }
}

还有 ScoreAdapter

public class ScoreAdapter extends RecyclerView.Adapter<ScoreViewHolder> {

    public List<ScoreDataViewModel> scores;

    public ScoreAdapter(List<ScoreDataViewModel> scores) {
        this.scores = scores;
    }

    @Override
    public ScoreViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        ItemScoreBinding itemScoreBinding = ItemScoreBinding.inflate(layoutInflater, parent, false);
        return new ScoreViewHolder(itemScoreBinding);
    }

    @Override
    public void onBindViewHolder(ScoreViewHolder holder, int position) {

        ScoreDataViewModel scoreDataViewModel = scores.get(position);
        holder.bind(scoreDataViewModel);
    }

    @Override
    public int getItemCount() {
        return scores.size();
    }
}

ViewHolder

public class ScoreViewHolder extends RecyclerView.ViewHolder {

    ItemScoreBinding itemScoreBinding;

    ScoreViewHolder(ItemScoreBinding itemScoreBinding) {
        super(itemScoreBinding.getRoot());
        this.itemScoreBinding=itemScoreBinding;
    }

    void bind(ScoreDataViewModel scoreDataViewModel) {
        itemScoreBinding.setScoreViewModel(scoreDataViewModel);
    }
}

现在,我的问题是为什么我看不到 recyclerview 项目。 当我在不使用空间的情况下设置数据时(不从数据库中获取数据而是静态创建和添加 ScoreDataViewModel 对象),它工作得很好。我哪里做错了?还是我做的方式不对?请帮助我,因为我是 room、rxjava 和 mvvm 的新手。

如果您提出要求,我也可以编辑问题并添加更多信息。请帮助我。

注意scoreAdapter.notifyDataSetChanged()方法无效。

public class ScoreAdapter extends RecyclerView.Adapter<ScoreViewHolder> {

    public List<ScoreDataViewModel> scores;

    public ScoreAdapter(List<ScoreDataViewModel> scores) {
        this.scores = scores;
    }

    //list Update
    public void setDataChange(List<ScoreDataViewModel> asList) {
        this.scores= asList;
        //now, tell the adapter about the update
        notifyDataSetChanged();
    }

    @Override
    public ScoreViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        ItemScoreBinding itemScoreBinding = ItemScoreBinding.inflate(layoutInflater, parent, false);
        return new ScoreViewHolder(itemScoreBinding);
    }

    @Override
    public void onBindViewHolder(ScoreViewHolder holder, int position) {

        ScoreDataViewModel scoreDataViewModel = scores.get(position);
        holder.bind(scoreDataViewModel);
    }

    @Override
    public int getItemCount() {
        return scores.size();
    }
}

也在 onStart()

上更改
mDisposable.add(scoreViewModel.getScoreDataViewModels()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<List<ScoreDataViewModel>>() {
                @Override
                public void accept(List<ScoreDataViewModel> scoreDataViewModels) throws Exception {
                    scoreDataViewModelList=scoreDataViewModels;
                    scoreAdapter.setDataChange(scoreDataViewModelList);
                    System.out.println(scoreDataViewModelList.toString());
                    Log.e("size", String.valueOf(scoreDataViewModelList));
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    Log.e("Unable to update list", throwable.toString());
                }
            }));
}