ListView 仅在刷新后显示项目

问题描述

我开始学习 android,有一次我遇到了类似的问题:在我的片段中,我有 editTextListView。我使用 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
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...