ViewModelFactory,填充父级和子级列表,排序不正确

时间:2019-02-01 06:37:57

标签: android android-mvvm

我正在开发一个应用程序,该应用程序允许用户拍摄收据的图片并将其保存到支出类别中。此刻,用户必须创建类别列表,然后单击工厂以输入收据。用户在该activity中选择一个类别,保存后,它应分类为相应的类别并出现在主fragment中。

为此,我创建了一个CategoryAdapter (recyclerview),它向recyclerview (ReceiptsAdapter)中的孩子onBindViewHolder膨胀。我使用Room将所有数据保存在database中。我也一直在加入ViewModel

问题:

  

我的问题是,当前所有收据项都填充到每个   类别,因为使用了ViewModel。当我使用可运行对象时   一切正常,除非没有任何东西可以保存到第一类。

下面始终是相关代码:

MainViewModel / CategoryViewModel

public class MainViewModel extends AndroidViewModel {

private LiveData<List<CategoryModel>> categories;

public MainViewModel(@NonNull Application application) {
    super(application);
    AppDatabase appDatabase = AppDatabase.getInstance(this.getApplication());
    categories = appDatabase.categoryDao().loadCategories();
}

public LiveData<List<CategoryModel>> getCategories() {
    return categories;
}
}

ReceiptsViewModel

public class ReceiptsViewModel extends ViewModel {

private LiveData<List<ReceiptEntity>> receipt;

public ReceiptsViewModel(AppDatabase database, String categoryName) {
    receipt = database.receiptDao().findReceiptsForCategory(categoryName);
}

public LiveData<List<ReceiptEntity>> getReceipt() {
    return receipt;
}
}

ReceiptsViewModelFactory

public class ReceiptsViewModelFactory extends ViewModelProvider.NewInstanceFactory {

private final String mCategoryName;
private final AppDatabase mDb;

public ReceiptsViewModelFactory(AppDatabase database, String categoryName) {
    mDb = database;
    mCategoryName = categoryName;
}

// Note: This can be reused with minor modifications
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
    // noinspection unchecked
    return (T) new ReceiptsViewModel(mDb, mCategoryName);
}
}

CategoryAdapter中的ViewHolder类

public class ViewHolder extends RecyclerView.ViewHolder {

    private TextView categoryTv;
    private RecyclerView receiptRv;

    private ViewHolder(View itemView) {
        super(itemView);
        categoryTv = itemView.findViewById(R.id.category_tv);
        receiptRv = itemView.findViewById(R.id.receipts_rv);
        receiptsAdapter = new ReceiptsAdapter(mContext, mReceiptslist, mListener);
    }

    public void setData(List<ReceiptEntity> list) {
        receiptsAdapter.setReceiptData(list);
    }
}

@Override
public void onBindViewHolder(final @NonNull CategoryAdapter.ViewHolder holder, final int i) {
    final String categoryName = mCategoriesList.get(i).getCategory();
    String categoryNamePlusColon = categoryName + ": ";
    holder.categoryTv.setText(categoryNamePlusColon);
    // Receipts viewModel
    ReceiptsViewModelFactory factory = new ReceiptsViewModelFactory(mDb, categoryName);
    final ReceiptsViewModel viewModel = ViewModelProviders.of((FragmentActivity) mContext, factory).get(ReceiptsViewModel.class);
    viewModel.getReceipt().observe((FragmentActivity) mContext, new Observer<List<ReceiptEntity>>() {
        @Override
        public void onChanged(@Nullable List<ReceiptEntity> receiptEntities) {
            holder.setData(receiptEntities);
        }
    });
    holder.receiptRv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
    holder.receiptRv.setHasFixedSize(true);
    holder.receiptRv.setNestedScrollingEnabled(true);
    holder.receiptRv.setAdapter(receiptsAdapter);
}

感谢您的帮助!让我知道是否还要发布其他内容。 〜谢谢〜

0 个答案:

没有答案