Recyclerview

时间:2018-04-05 09:31:49

标签: android rx-java android-databinding android-room android-mvvm

我正在学习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();
    }
}

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

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

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

2 个答案:

答案 0 :(得分: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());
                }
            }));
}

答案 1 :(得分:0)

.subscribe(new Consumer<List<ScoreDataViewModel>>() {
    @Override
    public void accept(List<ScoreDataViewModel> scoreDataViewModels) throws Exception {
        scoreDataViewModelList=scoreDataViewModels;

        // here 

        System.out.println(scoreDataViewModelList.toString());
        Log.e("size", String.valueOf(scoreDataViewModelList));
    }
 } 

呼叫

scoreAdapter.notifyDataSetChanged();