Cloud Firestore don't display data on Fragment

2019-08-26 09:38发布

问题:

I have created an app with Firebase Cloud Firestore. I'm trying to list my orders to a recyclerview in between fragments. I have a Fragment: OrderListFragment

// RecyclerView
mOrderAdapter = object : OrderAdapter(getQuery(mFirebaseFirestore), this@OrderListFragment) {
        override fun onDataChanged() {
            super.onDataChanged()

            mRecyclerView.visibility = View.VISIBLE

        }

        override fun onError(e: FirebaseFirestoreException) {
            super.onError(e)

            showShortToast(context!!, "Error: " + e.toString())

            // Show a snackbar on errors
            Snackbar.make(mSnackbarLayout!!, "Error: check logs for info.", Snackbar.LENGTH_LONG).show()
        }
    }
mRecyclerView.adapter = mOrderAdapter

and two Fragments extending the above Fragment:

1.

class RecentOrderFragment : OrderListFragment() {

    override fun getQuery(firebaseFirestore: FirebaseFirestore): Query {

        return firebaseFirestore.collection("tblOrders")
            .orderBy("timestamp", Query.Direction.DESCENDING)
            .whereEqualTo("isDraft", false)
            .whereEqualTo("isPaidFor", true)
            .limit(LIMIT.toLong())
    }
}

2.

class MyOrdersFragment : OrderListFragment() {

    override fun getQuery(firebaseFirestore: FirebaseFirestore): Query {
        return firebaseFirestore.collection("tblOrders")
            .whereEqualTo("userId", uid)
            .whereEqualTo("isDraft", false)
            .orderBy("timestamp", Query.Direction.DESCENDING)
            .limit(LIMIT.toLong())
    }
}

At first, it displays the orders, but when I swipe from one fragment, and go back, the previous fragment can't display the recyclerview again. Actually now it does not display at all