java—通过dao、repository和viewmodel从room数据库接收对象列表的问题找不到错误

4ngedf3f  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(281)

我在dao、repository、viewmodel和activity中都有方法来获取人员列表,但最后得到的是空列表(空对象引用错误)。哪里会出错?多谢。。
在persondao:

@Dao
public interface PersonDao {
    //other methods of PersonDao

    @Query("SELECT * FROM person_table WHERE status = :status ORDER BY RANDOM() LIMIT 5")
    List<Person> getFivePersonsFrom(String status);
}

在personrepository中:

public class PersonRepository {
    private PersonDao mPersonDao;
    private List<Person> mFivePersonsFrom;

    //other methods

    List<Person> getFivePersonsFrom(String status) {
        PersonRoomDatabase.databaseWriteExecutor.execute(() -> {
            mPersonDao.getFivePersonsFrom("noob");
        });
        return mFivePersonsFrom;
    }
}

在personviewmodel中:

public class PersonViewModel extends AndroidViewModel {
    private PersonRepository mRepository;
    public List<Person> mFivePersonsFrom;
    public PersonViewModel(@NonNull Application application) {
        super(application);
        mRepository = new PersonRepository(application);
        mFivePersonsFrom = mRepository.getFivePersonsFrom("noob");
        //other methods
    }
    public List<Person> getFivePersonsFrom() {
        mRepository.getFivePersonsFrom("noob");
        return mFivePersonsFrom;
    }  
}

主要活动:

private CardStackView noobCardStackView;
private NoobAdapter noobAdapter;
List<Person> noobList;     
// other
protected void onCreate(Bundle savedInstanceState) {
    noobViewModel = new ViewModelProvider(this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication()))
            .get(PersonViewModel.class);
    noobList = noobiewModel.getFivePersonsFrom();
noobAdapter = new NoobAdapter(new NoobAdapter.NoobDiff(), noobList); 
    noobCardStackView.setAdapter(noobAdapter);
    // methods
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题