0

我正在从 Cloud Firestore 加载数据。然后将该数据作为 Item 添加到 RecyclerView 并应显示在屏幕上。但是,RecyclerView 仍然是空的。

数据已从 Cloud Firestore 正确加载(我可以知道,因为我添加了如下所示的日志)。

所以我无法找出为什么数据没有正确添加到 RecyclerView 并显示?

ActiveOrderActivity.kt

class ActiveOrderActivity : AppCompatActivity() {
    private val aorderList = ArrayList<ActiveOrderModel>()
    private val adapter = AOrdersAdapter(aorderList)

    /* Access a Cloud Firestore instance from the Activity. */
    val db = Firebase.firestore

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_active_order)

        recyclerview.adapter = adapter
        recyclerview.layoutManager = LinearLayoutManager(this)
        recyclerview.setHasFixedSize(true)

        db.collection("Orders - 18.3.2021")
            .get()
            .addOnSuccessListener { result ->
                for (document in result) {
                    Log.i("", "IN LOOP FOR DOCUMENT: ActiveOrderActivity.\n")
                    val customerName = document.getField<String>("customer Name")
                    Log.i("", "$customerName: ActiveOrderActivity.\n")
                    val customerNumber = document.getField<String>("customer Number")
                    val customerPostal = document.getField<String>("eircode")
                    val customerAddress = document.getField<String>("address")
                    val paymentAmount = document.getField<String>("payment Amount")
                    val paymentType =  document.getField<String>("payment Total")
                    val newItem = ActiveOrderModel(customerName = customerName,customerNumber = customerNumber,customerPostal = customerPostal,customerAddress = customerAddress,paymentAmount = paymentAmount,paymentType = paymentType)
                    aorderList.add(INDEX,newItem)
                    adapter.notifyItemInserted(INDEX)
                }
            }
   
    }

}

AOrdersAdapter.kt


class AOrdersAdapter(private val aorderList: List<ActiveOrderModel> ) : RecyclerView.Adapter<AOrdersAdapter.AOrderViewHolder>() {

    class AOrderViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val nameView = view.nameView
        val addressView = view.address1View
        val mobileView = view.mobileView
        val eircodeView = view.eircodeView
        val paymentView = view.paymentView
        val paymentAmountView = view.paymentAmountView
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AOrderViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.active_order_item, parent, false)

        return AOrderViewHolder(view)
    }

    override fun getItemCount(): Int {
        return aorderList.size
    }

    override fun onBindViewHolder(holder: AOrderViewHolder, position: Int) {
        val currentItem = aorderList[position]

        holder.nameView.text = currentItem.customerName
        holder.addressView.text = currentItem.customerNumber
        holder.mobileView.text = currentItem.customerPostal
        holder.eircodeView.text = currentItem.customerAddress

        holder.paymentAmountView.text = currentItem.paymentAmount
        holder.paymentView.text = currentItem.paymentType
    }
}

activity_active_order.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainBody.dashboard.ordersActivites.ActiveOrderActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="wrap_content"
        android:layout_height="699dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="16dp"
        android:clipToPadding="false"
        android:padding="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/floatingActionButton"
        app:layout_constraintVertical_bias="0.0"
        tools:listitem="@layout/active_order_item" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/floatingActionButton"
        android:layout_width="307dp"
        android:layout_height="68dp"
        android:layout_marginTop="88dp"
        android:background="@drawable/pizaa_button2"
        android:text="Main Menu"
        android:textColor="@color/white"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

active_order_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    android:id="@+id/rowConstraintLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:padding="16dp"
        android:layout_margin="4dp"
        app:cardElevation="10dp"
        app:cardCornerRadius="8dp"
        app:cardPreventCornerOverlap="false">

        <RelativeLayout
            android:layout_width="800dp"
            android:layout_height="match_parent"
            android:padding="12dp">

            <TextView
                android:id="@+id/nameView"
                android:layout_width="223dp"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_marginStart="6dp"
                android:layout_marginEnd="0dp"
                android:text="George Matthews"
                android:textColor="@color/shopColour"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/address1View"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/nameView"
                android:layout_alignParentStart="true"
                android:layout_marginStart="6dp"
                android:layout_marginLeft="2dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="6dp"
                android:text="123 Fakelands,\nHigher up Road,\nDublin"
                android:textColor="@color/logoYellow"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/mobileView"
                android:layout_width="144dp"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/productPriceView"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="2dp"
                android:layout_marginTop="1dp"
                android:layout_marginEnd="100dp"
                android:layout_toEndOf="@+id/nameView"
                android:text="089 215 2121"
                android:textColor="@color/shopColour"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/eircodeView"
                android:layout_width="87dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/mobileView"
                android:layout_alignEnd="@+id/mobileView"
                android:layout_marginStart="43dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="11dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="1dp"
                android:layout_toEndOf="@+id/nameView"
                android:text="A96 K4D8"
                android:textColor="@color/logoYellow"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/paymentView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/productPriceView"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="2dp"
                android:layout_marginTop="1dp"
                android:layout_marginEnd="100dp"
                android:layout_toEndOf="@+id/mobileView"
                android:gravity="center"
                android:text="Frank's Website"
                android:textColor="@color/shopColour"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/paymentAmountView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/paymentView"
                android:layout_alignEnd="@+id/paymentView"
                android:layout_marginStart="144dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="12dp"
                android:layout_marginEnd="-5dp"
                android:layout_marginBottom="1dp"
                android:layout_toEndOf="@+id/eircodeView"
                android:gravity="center"
                android:text="47.00"
                android:textColor="@color/logoYellow"
                android:textSize="20sp"
                android:textStyle="bold" />

        </RelativeLayout>
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

ActiveOrderModel.kt

data class ActiveOrderModel(
    val customerName: String? = null,
    val customerNumber: String? = null,
    val customerPostal: String? = null,
    val customerAddress: String? = null,
    val paymentAmount: String? = null,
    val paymentType: String? = null )

这里运行的终端显示数据已从 Cloud Firestore 正确加载,并且循环正在迭代集合中的所有条目。现在每个文档都应该添加到 RecyclerView 但 Recycler View 是空的,如下所示。 在此处输入图像描述

在此处输入图像描述

4

2 回答 2

1

您需要更新适配器中的列表以获得所需的结果。在适配器中创建一个方法,如下所示 -

fun updateList(list: List<ActiveOrderModel> ) {
    this.aorderList = list
    notifyDataSetChanged()
}

在您的活动中更新以下行

aorderList.add(INDEX,newItem)
adapter.updateList(aorderList)
adapter.notifyItemInserted(INDEX)
于 2021-03-18T12:36:18.317 回答
0

尝试这个

fun initRecyclerView() {
    mainRecycler.setLayoutManager(LinearLayoutManager(context))
    var linearLayoutManager: LinearLayoutManager? = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
    mainRecycler?.layoutManager = linearLayoutManager
    val itemDecorator = VerticalSpacingItemDecorator(20)
    mainRecycler.addItemDecoration(itemDecorator)
    adapter = AOrdersAdapter(aorderList)
    mainRecycler.setAdapter(adapter)
}

和这个

for (document in result) {
                Log.i("", "IN LOOP FOR DOCUMENT: ActiveOrderActivity.\n")
                val customerName = document.getField<String>("customer Name")
                Log.i("", "$customerName: ActiveOrderActivity.\n")
                val customerNumber = document.getField<String>("customer Number")
                val customerPostal = document.getField<String>("eircode")
                val customerAddress = document.getField<String>("address")
                val paymentAmount = document.getField<String>("payment Amount")
                val paymentType =  document.getField<String>("payment Total")
                val newItem = ActiveOrderModel(customerName = customerName,customerNumber = customerNumber,customerPostal = customerPostal,customerAddress = customerAddress,paymentAmount = paymentAmount,paymentType = paymentType)
                aorderList.add(INDEX,newItem)
                adapter.notifyItemInserted(INDEX)
            }
            adapter?.notifyDataSetChanged()

使用 adapter?.notifyDataSetChanged()
并且必须检查您的数组列表不为空。

于 2021-03-18T12:48:14.880 回答