Android 带房间的AutocompleteTextView
我有一个房间数据库,并创建了一个模型和视图模型。我想知道如何使autocompletetextview与数据库数据和viewmodel一起工作,以便根据用户类型过滤客户列表 视图模型Android 带房间的AutocompleteTextView,android,kotlin,android-room,Android,Kotlin,Android Room,我有一个房间数据库,并创建了一个模型和视图模型。我想知道如何使autocompletetextview与数据库数据和viewmodel一起工作,以便根据用户类型过滤客户列表 视图模型 class CustomerVM : ViewModel() { private val customers: MutableLiveData<List<Customer>> by lazy { loadCustomers() } fun getCu
class CustomerVM : ViewModel() {
private val customers: MutableLiveData<List<Customer>> by lazy {
loadCustomers()
}
fun getCustomers(): LiveData<List<Customer>> {
return customers
}
private fun loadCustomers() : MutableLiveData<List<Customer>> {
return DataModel.getInstance().roomDb.CustomerDao().getAllCustomers() as MutableLiveData<List<Customer>>
}
}
class CustomerVM:ViewModel(){
私有val客户:lazy提供的MutableLiveData{
装载客户()
}
fun getCustomers():LiveData{
回头客
}
private fun loadCustomers():MutableLiveData{
返回DataModel.getInstance().roomDb.CustomerDao().getAllCustomers()作为MutableLiveData
}
}
步骤1:创建自定义自动完成文本视图
例子:https://spapas.github.io/2019/04/05/android-custom-filter-adapter/
步骤2:从房间dp获取数据
@查询(“从性别中选择*,其中name==:name”)
有趣的getGenderByName(名称:String):列表
步骤3:将此数据更新为自定义自动完成适配器
步骤1:创建自定义自动完成文本视图
例子:https://spapas.github.io/2019/04/05/android-custom-filter-adapter/
步骤2:从房间dp获取数据
@查询(“从性别中选择*,其中name==:name”)
有趣的getGenderByName(名称:String):列表
步骤3:将此数据更新为自定义自动完成适配器
如果我知道您希望将内部数据库中的数据作为自动完成文本的条目查看
为此,我使用allStudentsData、存储库和listen from活动创建了自定义ViewModel
val-allStudents:LiveData=studentDao.getAll()
val studentsDao = AppDatabase.getDatabase(application,viewModelScope).studentsDao()
studentRepository = StudentRepository(studentsDao)
allStudents = studentRepository.allStudents
}`
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
studentViewModel=ViewModelProvider(this).get(studentViewModel::class.java)
studentViewModel.allStudents.observe(这个,observator{students->
//更新适配器中学生的缓存副本。
学生们?让我来{
val arr=mutableListOf()
为了(其中的价值){
arr.add(value.name)
}
val适配器:阵列适配器=
ArrayAdapter(此,android.R.layout.select_对话框_项,arr)
names.setAdapter(适配器)
}
})
}DB==>存储库===>视图模型==>活动如果我了解您希望将自动完成文本作为条目,请查看内部DB中的数据 为此,我使用allStudentsData、存储库和listen from活动创建了自定义ViewModel
val-allStudents:LiveData=studentDao.getAll()
val studentsDao = AppDatabase.getDatabase(application,viewModelScope).studentsDao()
studentRepository = StudentRepository(studentsDao)
allStudents = studentRepository.allStudents
}`
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
studentViewModel=ViewModelProvider(this).get(studentViewModel::class.java)
studentViewModel.allStudents.observe(这个,observator{students->
//更新适配器中学生的缓存副本。
学生们?让我来{
val arr=mutableListOf()
为了(其中的价值){
arr.add(value.name)
}
val适配器:阵列适配器=
ArrayAdapter(此,android.R.layout.select_对话框_项,arr)
names.setAdapter(适配器)
}
})
}DB===>存储库===>视图模型==>活动执行此操作。
val适配器=AutocompleteArrayAdapter(上下文!!,R.layout.location\u下拉菜单\u自动完成,您的客户列表)您的自动完成。setAdapter(适配器)
执行此操作。val adapter=AutocompleteArrayAdapter(context!!,R.layout.location\u drop\u down\u autocomplete,yourListOfCustomers)your autocomplete.setAdapter(adapter)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
studentViewModel = ViewModelProvider(this).get(StudentViewModel::class.java)
studentViewModel.allStudents.observe(this, Observer { students ->
// Update the cached copy of the students in the adapter.
students?.let {
val arr = mutableListOf<String>()
for (value in it) {
arr.add(value.name)
}
val adapter: ArrayAdapter<String> =
ArrayAdapter(this, android.R.layout.select_dialog_item, arr)
names.setAdapter(adapter)
}
})