我正在研究房间livedata数据绑定概念。我正在使用自定义绑定适配器将列表设置为微调框。但是我第一次从房间检索时,得到的实时数据为空。
代码:
@Query("select * from Student")
LiveData<List<Student>> getAllItems();
@BindingAdapter({"bind:entries"})
public static void customSpinnerBinding(AppCompatSpinner appCompatSpinner,LiveData<List<Student>> listLiveData){
if(listLiveData != null && listLiveData.getValue().size() > 0){
ArrayAdapter<Student> spinnerArrayAdapter = new ArrayAdapter<Student>
(appCompatSpinner.getContext(), android.R.layout.simple_spinner_item,
listLiveData.getValue()); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
appCompatSpinner.setAdapter(spinnerArrayAdapter);
}
}
public LiveData<List<Student>> getListLiveData() {
if(listLiveData == null){
listLiveData = studentDB.studentDao().getAllItems();
}
return listLiveData;
}
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spinner"
android:layout_width="0dp"
bind:entries="@{viewmodel.listLiveData}"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
but if i observe livedata in activity this is working
mainViewModel.getListLiveData().observe(this, new Observer<List<Student>>() {
@Override
public void onChanged(@Nullable List<Student> students) {
if(students.size() > 0){
ArrayAdapter<Student> spinnerArrayAdapter = new ArrayAdapter<Student>
(MainActivity.this, android.R.layout.simple_spinner_item,
students); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
activityMainBinding.appbar.contentid.spinner.setAdapter(spinnerArrayAdapter);
}
}
});
答案 0 :(得分:0)
尝试
listLiveData = StudentDB.getAppDatabase(application).studentDao().getAllItems();
return listLiveData;
答案 1 :(得分:0)
检查以下两项:
@for $i from 1 through 20 {
// the hash forces SCSS to create a string
.item:nth-child(#{$i}){
// you need to use $i for BOTH since its the only defined increment
animation-delay: $i * $timeOffset, $i * $aniLength;
// delay #1 = iteration * 0.5s, delay #2 = iteration * 2s
}
}
更改为接受.customSpinnerBinding
而不是List<Student>
。 LiveData<List<Student>>
接受BindingAdapter
内部的内容,而不接受LiveData
本身。 LiveData
不会改变。仅LiveData
通知的内容会更改。
BindingAdapter示例:
LiveData
@BindingAdapter({"bind:entries"})
public static void customSpinnerBinding(AppCompatSpinner appCompatSpinner, List<Student> students) {
ArrayAdapter<Student> spinnerArrayAdapter = new ArrayAdapter<Student>
(appCompatSpinner.getContext(), android.R.layout.simple_spinner_item,
students); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
appCompatSpinner.setAdapter(spinnerArrayAdapter);
}
您的视图似乎仍然需要它,延迟加载的目的是什么?通过使您的视图模型更有状态,这只会使您的代码更复杂且更难调试。只是热切地加载学生名单,并将其声明为最终名单,以使其变得更好。
listLiveData
如果必须延迟加载public final LiveData listLiveData = studentDB.studentDao().getAllItems();
,请确保它已初始化。在您的.xml中:
listLiveData