1

我正在学习 mvvm 结构,并使用 mvvm 结构制作了一个应用程序。我也用过房间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());
                    }
                }));
    }
}

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

活动分数

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

视图持有者

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 对象)。我哪里做错了?或者我这样做的方式是错误的?请帮助我,因为我对房间、rxjava 和 mvvm 完全陌生。

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

注意scoreAdapter.notifyDataSetChanged()方法不起作用。

4

1 回答 1

1
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());
                }
            }));
}
于 2018-04-05T09:36:46.670 回答