我无法让微调器正确处理从SQLite数据库中检索到的项目。我可以在下拉菜单中显示项目,但无法选择它们。如果我用简单的字符串数组替换了项目(questAddViewModel.categorylist),则微调器将正常工作。我只是无法弄清楚如何从数据库中填充列表以代替工作。
我正在使用会议室和Dao进行数据库调用。
更新:随附的屏幕截图用于说明。如果我单击第二张图片中的任何项目,则屏幕仅返回到第一张图片,而在微调框中未显示任何选择。如果我像在注释代码中一样尝试将selectedItem分配给一个字符串
window is not defined
at eval (eval at compile (\node_modules\ejs\lib\ejs.js:618:12), <anonymous>:215:37)
at returnedFn (\node_modules\ejs\lib\ejs.js:653:17)
at tryHandleCache (\node_modules\ejs\lib\ejs.js:251:36)
at View.exports.renderFile [as engine] (\node_modules\ejs\lib\ejs.js:482:10)
at View.render (\node_modules\express\lib\view.js:135:8)
at tryRender (\node_modules\express\lib\application.js:640:10)
at Function.render (\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (\node_modules\express\lib\response.js:1012:7)
at \controllers\productController.js:1274:8
at \node_modules\mongoose\lib\model.js:4733:16
at \node_modules\mongoose\lib\utils.js:263:16
at \node_modules\mongoose\lib\aggregate.js:973:13
at \node_modules\kareem\index.js:135:16
at processTicksAndRejections (internal/process/task_queues.js:82:9)
它会告诉我它为空。
这是片段中微调器的代码:
//questCategorySelected = parent.getItemAtPosition(pos).toString()
这是我在ViewModel中的代码
//Setup Spinner for Quest Category
val categorySpinnerAdapter = ArrayAdapter(application, android.R.layout.simple_spinner_item, questAddViewModel.categorylist) //
categorySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
binding.addQuestCategory.adapter = categorySpinnerAdapter
binding.addQuestCategory.onItemSelectedListener = object : AdapterView.OnItemSelectedListener
{
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
//questCategorySelected = parent.getItemAtPosition(pos).toString()
//Toast.makeText(application, "${parent.getItemAtPosition(pos).toString()} <-In Selected", Toast.LENGTH_LONG).show();
}
override fun onNothingSelected(parent: AdapterView<*>) {
// Another interface callback
}
}
这就是我在道中所拥有的
private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
val categorylist = ArrayList<String>()
init {
populateCategorySpinner()
}
fun populateCategorySpinner()
{
uiScope.launch {
populateCategorySpinnerList()
}
}
private suspend fun populateCategorySpinnerList()
{
withContext(Dispatchers.IO)
{
val categorylistcursor = database.getQuestCategoryCursor()
if (categorylistcursor.moveToFirst()) {
do {
val category =
categorylistcursor.getString(categorylistcursor.getColumnIndexOrThrow("quest_category_name"))
categorylist.add(category);
} while (categorylistcursor.moveToNext())
}
categorylistcursor.close();
}
}
我正在寻找一种解决方案,以获取从数据库游标检索到的列表,以便能够在微调器中正确选择。
答案 0 :(得分:0)
您是否尝试过更改 questCategorySelected = parent.getItemAtPosition(pos).toString()
通过
questCategorySelected =(TextView)视图
答案 1 :(得分:0)
感谢您的评论和回答。在阅读评论后,我实际上发现了这一点。似乎问题是由于对数据库的协程调用,实际上数组未填充数据。这很混乱,因为它显示在下拉列表中。在阅读了LiveData的更多内容并保持适配器更新后,我解决了该问题。我尝试了几种将数组从ViewModel传递到Fragment的方法,但是好像没有使用LiveData,由于数据库调用协程,列表总是丢失。如果有人有更好的方法,欢迎提出建议!谢谢!
以下是我的解决方法:
片段
//Setup Spinner for Quest Category with LiveData monitoring of the Category List
var CategoryArray: Array<String>
var categorySpinnerAdapter: ArrayAdapter<String>
questAddViewModel.categorylist.observe(this, Observer
{if (it != null && it.isNotEmpty()) {
CategoryArray = toArray(questAddViewModel.categorylist.value!!)
categorySpinnerAdapter = ArrayAdapter(application, android.R.layout.simple_spinner_item, CategoryArray) //
categorySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
binding.addQuestCategory.adapter = categorySpinnerAdapter
}})
binding.addQuestCategory.onItemSelectedListener = object : AdapterView.OnItemSelectedListener
{
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
override fun onNothingSelected(parent: AdapterView<*>) {
// Another interface callback
}
}
ViewModel
private val _categorylist = MutableLiveData<ArrayList<String>>()
val categorylist: LiveData<ArrayList<String>>
get() = _categorylist
private suspend fun populateCategorySpinnerList()
{
withContext(Dispatchers.IO)
{
val categorylistcursor = database.getQuestCategoryCursor()
val categorylistfromdb = ArrayList<String>()
if (categorylistcursor.moveToFirst()) {
do {
val category =
categorylistcursor.getString(categorylistcursor.getColumnIndexOrThrow("quest_category_name"))
categorylistfromdb.add(category);
} while (categorylistcursor.moveToNext())
}
categorylistcursor.close();
_categorylist.postValue(categorylistfromdb)
}
}