0

I have problem with fetching all data in ArrayList inside collection, its only show last added element everytime, but i need show full list inside RecyclerView of all data.Tried a lot of things, but it doesn't help. Thanks in advance!

Collection Screenshot

Demonstration of problem

Data Model

data class  Holder(
    val id : String? = null,
    val holderHistoryItem: ArrayList<HolderHistItem> = arrayListOf()
data class HolderHistItem(
    val id : String? = null,

    val instrName: String? = null,

    val getTime: String? = null,

    val returnTime: String? = null,
)

Function of fetching

 @ExperimentalCoroutinesApi
    fun getAllInstrumentInHolderHistory(holder: Holder): Flow<State<List<ArrayList<HolderHistItem>>>> =
        callbackFlow {
            val snapshotListener = holderCollection
                .orderBy("name")
                .whereEqualTo("id",holder.id)
                .addSnapshotListener { value , error ->
                    val response = if (error == null && value != null) {
                        trySend(State.loading())
                        val data = value.documents.mapNotNull { doc ->
                            doc.toObject(Holder::class.java)?.holderHistoryItem
                        }
                        State.success(data)
                    } else {
                        State.failed(error.toString())
                    }
                    trySend(response)
                }
            awaitClose {
                snapshotListener.remove()
            }
        }

List Adapter

class HolderHistAdapter : ListAdapter<ArrayList<HolderHistItem>,HolderHistAdapter.ItemHist>(ItemComparator()) {
    class ItemHist(view: View) : RecyclerView.ViewHolder(view) {

        private val b = HistitemItemBinding.bind(view)

        @SuppressLint("SetTextI18n")
        fun setData(holderHistItem: ArrayList<HolderHistItem>) = with(b){
                               holderHistItem.forEach {
                                   tvListName.text = it.instrName.toString()
                                   tvGet.text = it.getTime.toString()
                                   tvReturn.text = it.returnTime.toString()
                               }
        }
        companion object {
            fun create(parent: ViewGroup): HolderHistAdapter.ItemHist {
                return HolderHistAdapter.ItemHist(
                    LayoutInflater.from(parent.context)
                        .inflate(R.layout.histitem_item, parent, false)
                )
            }
        }
    }

    class ItemComparator : DiffUtil.ItemCallback<ArrayList<HolderHistItem>>() {
        override fun areItemsTheSame(oldItem: ArrayList<HolderHistItem>, newItem: ArrayList<HolderHistItem>): Boolean {
            return oldItem.hashCode() == newItem.hashCode()
        }

        override fun areContentsTheSame(oldItem: ArrayList<HolderHistItem>, newItem:ArrayList<HolderHistItem>): Boolean {
            return oldItem == newItem
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHist {
        return ItemHist.create(parent)

    }

    override fun onBindViewHolder(holder: ItemHist, position: Int) {
        holder.setData(getItem(position))
    }
}

Load data in adapter

 fun observHistoryItem(){
        mainViewModel.getAllInstrHistoryItem(holder!!).observe(viewLifecycleOwner,{
            when (it) {
                is State.Loading -> {

                }
                is State.Success -> {
                    if(it.data.isEmpty()){
                        b.rcHistItem.visibility = View.GONE
                        b.tvEmptyHist.visibility = View.VISIBLE
                    }else {
                        adapterHis.submitList(it.data)
                        b.rcHistItem.visibility = View.VISIBLE
                    }
                }
                is State.Failed -> {

                    Log.d("Error" , "Error name :${it.message}")
                }
            }

        })
    }
anderson48
  • 35
  • 1
  • 7
  • Have you created an [index](https://stackoverflow.com/questions/50305328/firestore-whereequalto-orderby-and-limit1-not-working)? – Alex Mamo Jun 03 '22 at 05:14
  • @AlexMamo Hello! What do u mean index? In Recyclerview? Or better show me from code) – anderson48 Jun 03 '22 at 06:38
  • Have you tried to implement the solution in the linked answer? You need to add it to the Firebase Console, **not** in the RecyclerView. – Alex Mamo Jun 03 '22 at 07:17
  • @AlexMamo Yes, i checked in Logs, i have this error, later will try. Thanks a lot!) Can you help please with another question, its work with rules. Briefly tell you, that i want to get access read only if documents have the same values of fields in different collections. => https://stackoverflow.com/questions/72274946/firebase-rules-how-to-get-and-compare-fileds-values-from-collections – anderson48 Jun 03 '22 at 09:49
  • @Alex Mamo Hi again! Fix all these errors, clicking by links and added indexes, but anyway don't show all data inside arraylist – anderson48 Jun 04 '22 at 12:05

0 Answers0