简体   繁体   English

无法将值从 Activity 移交给 Repository 以将其传递给 Dao 方法

[英]Cannot handover value from Activity to Repository to pass it into Dao method

I have a vocabulary room-database where words are divided into 4 categories (indicated in respective field of the table).我有一个词汇室数据库,其中单词分为 4 个类别(在表格的各个字段中表示)。

When the Activity launches, the method getWords() from Dao is triggered with the required argument being passed by WorkActivity当 Activity 启动时,来自 Dao 的方法getWords()被触发,所需的参数由WorkActivity传递

On launching the Activity I get the following error在启动活动时,我收到以下错误

Cannot create an instance of class space.rodionov.englishwanker.WordViewModel.

Where did I go wrong?我在哪里 go 错了? Any help is appreciated任何帮助表示赞赏

In Dao:在道:

@Query("SELECT * FROM word_table WHERE category IN(:filterCategory) ORDER BY RANDOM() LIMIT 4")
    Single<List<Word>> get4words(List<Integer> filterCategory);

In Repository:在存储库中:

public class WordRepository {
    private ArrayList<Integer> categoryFilter;
    private WordDao mWordDao;   
    //other variables
    WordRepository(Application application) {
        WordDatabase db = WordDatabase.getDatabase(application);
        mWordDao = db.WordDao();
        clearFilter();
        setFilter(categoryFilter);
        m4words = mWordDao.get4words(categoryFilter);
    }
    public void clearFilter(){
        categoryFilter.clear();
    }

    public void setFilter(ArrayList<Integer> categoryFilter) {
        this.categoryFilter = categoryFilter;
    }
    

In ViewModel:在视图模型中:

private ArrayList<Integer> categoryFilter = new ArrayList<>();
private WordRepository mRepository;
// declaring variables
public WordViewModel(@NonNull Application application) {
    mRepository = new WordRepository(application);
    //declaring
    mRepository.setFilter(categoryFilter);
    m4words = mRepository.get4words();
}
public void clearFilter() {
    categoryFilter.clear();
}
public void setFilter(ArrayList<Integer> categoryFilter) {
    this.categoryFilter = categoryFilter;
}
//other methods

In WorkActivity:在工作活动中:

private WordViewModel wordViewModel;
public ArrayList<Integer> categoryFilter = new ArrayList<>();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPreferences = getSharedPreferences("save", MODE_PRIVATE);
        boolean category0 = sharedPreferences.getBoolean("Category0", true);
        boolean category1 = sharedPreferences.getBoolean("Category1", true);
        //other catigories from memory ... 
        //
        wordViewModel = new ViewModelProvider(this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication()))
            .get(WordViewModel.class);
        wordViewModel.getListLivedata().observe(this, words -> {
            adapter.submitList(words);
        });
        //
        wordViewModel.clearFilter();
        if (category0 == true) {
            categoryFilter.add(0);
            Log.d(TAG, "onCreate: common_words added: called");
        }
        //other categories
        wordViewModel.setFilter(categoryFilter);
        //other methods
        //buildRecyclreVeiw
    }

Answer is I just forgot to initialize ArrayList categoryFilter in Repository.答案是我只是忘记在存储库中初始化ArrayList categoryFilter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android体系结构:从活动中调用dao方法并将结果传递给子活动以进行过滤 - android architecture: call dao method from activity and pass result to child activity for filtering 无法移交列表<obj> (来自单<list<obj> &gt;) 从 ViewModel 到 Activity。 RecyclerView 保持为空。 原因在哪里? </list<obj></obj> - Cannot handover List<Obj> (got from Single<List<Obj>>) from ViewModel to Activity. RecyclerView stays empty. Where is the cause? 将对象的值从DAO传递到2D数组中的servlet - Pass value of an object from DAO to servlet in 2D array 无法将数据从asyncTask传递到我的活动 - Cannot pass data from asyncTask to my activity 将字符串从 onLoadFinished 方法传递到另一个活动 - Pass string from onLoadFinished method to another activity 什么参数需要传递给DAO方法调用 - what param need to pass to DAO method calling 如何将一个活动的edittext字符串传递给另一个活动中的方法? - How to pass an edittext string from an activity to a method in another activity? Room dao 无法解析表名和方法 - Room dao cannot resolve table name and method 无法从活动中调用RecyclerView适配器的方法 - Cannot call method of RecyclerView adapter from an activity 如何从方法传递 EditText 值以在同一活动中为 SQLiteOpenHelper class 插入 function? - How to pass an EditText Value from a method to Insert function for SQLiteOpenHelper class in same Activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM