ListView 仅在刷新后显示项目
ListView show item only after refresh
我开始学习 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(), android.R.layout.simple_list_item_1, arrayOfNames)
root.findViewById<ListView>(R.id.for_list_of_categories).adapter = adapter
}
return root
}
或adapter.notifyDatasetChanged()
在回调中。
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
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
arrayOfNames = ArrayList()
db.collection("restaurants")
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
arrayOfNames.add(document.data["name"].toString())
}
adapter.notifyDataSetChanged()
}
return root
}
我开始学习 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(), android.R.layout.simple_list_item_1, arrayOfNames)
root.findViewById<ListView>(R.id.for_list_of_categories).adapter = adapter
}
return root
}
或adapter.notifyDatasetChanged()
在回调中。
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
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
arrayOfNames = ArrayList()
db.collection("restaurants")
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
arrayOfNames.add(document.data["name"].toString())
}
adapter.notifyDataSetChanged()
}
return root
}