如何解决ListView 仅在刷新后显示项目
我开始学习 android,有一次我遇到了类似的问题:在我的片段中,我有 editText
和 ListView
。我使用 ArraList<String>
数据设置了标准适配器,并看到 ListView
仅在我调用键盘点击 editText
后才显示项目。我试图在许多编程网站上找到有关它的任何信息,但都失败了。
我遇到了什么问题?
我的 .xml 文件:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.forMainActivity.searchdish.SearchShopFragment"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayoutCompat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="10dp"
android:weightSum="5">
<EditText
android:id="@+id/for_search_shop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_baseline_search_24" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/for_list_of_categories"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
我的片段文件:
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View? {
arrayOfNames = ArrayList()
db.collection("restaurants")
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
arrayOfNames.add(document.data["name"].toString())
}
}
val root = inflater.inflate(R.layout.fragment_search_shop,container,false)
val adapter = ArrayAdapter(requireContext(),android.R.layout.simple_list_item_1,arrayOfNames)
root.findViewById<ListView>(R.id.for_list_of_categories).adapter = adapter
return root
}
解决方法
您需要使用数据同步更新适配器,因为在设置适配器后会调用 addOnSuccessListener
回调,因此在下面的代码行中,arrayOfNames
列表仍然是空的。
val adapter = ArrayAdapter(requireContext(),android.R.layout.simple_list_item_1,arrayOfNames)
要解决此问题,您需要:
在 addOnSuccessListener
回调中设置适配器
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_search_shop,container,false)
arrayOfNames = ArrayList()
db.collection("restaurants")
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
arrayOfNames.add(document.data["name"].toString())
}
val adapter = ArrayAdapter(requireContext(),arrayOfNames)
root.findViewById<ListView>(R.id.for_list_of_categories).adapter = adapter
}
return root
}
或回调中的 adapter.notifyDatasetChanged()
。
override fun onCreateView(
inflater: LayoutInflater,false)
val adapter = ArrayAdapter(requireContext(),arrayOfNames)
root.findViewById<ListView>(R.id.for_list_of_categories).adapter = adapter
arrayOfNames = ArrayList()
db.collection("restaurants")
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
arrayOfNames.add(document.data["name"].toString())
}
adapter.notifyDataSetChanged()
}
return root
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。