微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ListView 仅在刷新后显示项目

如何解决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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?