Home > Enterprise >  When i get 10 data next 10 data is downloading but the previous 10 data is disappear how can i fix?(
When i get 10 data next 10 data is downloading but the previous 10 data is disappear how can i fix?(

Time:10-18

OnCreate.............................................................................................................................................................................................................................................................................................................................

  auth= FirebaseAuth.getInstance()
      adapters= ImageAdp(ArrayList()) 
      binding.idRLWallpapers.adapter=adapters
      getUsers(null)
    
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                var linearLayoutManager:LinearLayoutManager= recyclerView.layoutManager as LinearLayoutManager
                var currentItems=linearLayoutManager.childCount
                total_item=linearLayoutManager.itemCount
                var lastVisibleitem=linearLayoutManager.findFirstVisibleItemPosition()
                if(!isLoadingEDMT&&total_item<=lastVisibleitem ITEM_COUNT){


                       // misLoad=true

                        getUsers(adapters.lastItemId())


                    isLoadingEDMT=true

                        binding.bottomPB.visibility=View.GONE

                        isLoad=false

                }
            }

        })

This is how i am fetching data from firebase database .

private fun getUsers(nodeId:String?) {
if (nodeId==null){
                    Query = database.orderByKey().limitToFirst(ITEM_COUNT)
                }
                 else{
                    Query = database.orderByKey().startAfter(nodeId).limitToFirst(ITEM_COUNT)
                }

             

 Query.addValueEventListener(object : ValueEventListener {
             
                    override fun onDataChange(snapshot: DataSnapshot) {
                       var arrayLists=ArrayList<imagemodel>()
                        if (snapshot.hasChildren()) {

                            for (data in snapshot.children) {

                                val image = data.child("url2").value.toString()
                                val name = data.child("voice1").value.toString()
                                val childname = data.child("childname").value.toString()
                                val id = data.child("id").value.toString()
                                val model = imagemodel(image, name, 

                                arrayLists.add(model)
                            }

                            //adapters= ImageAdp(arrayList)
                            //binding.idRLWallpapers.adapter=adapters

                            adapters.addAll(arrayLists)

This is my RecyclerView Adapter(I didn't put any more to avoid confusion)

fun addAll(emp:List<imagemodel>){

    var initialSize= myarraylist.size
    myarraylist.addAll(emp)
    notifyDataSetChanged()

}
fun lastItemId(): String? {
   return myarraylist.get(myarraylist.size-1).uid
}

Thank you for your time

CodePudding user response:

As i understand, you are initializing your adapter in OnCreate and trying to initialize again for every 10 element in down below, and last one replaces the previous one.
Initialize adapter with empty arraylist in OnCreate:

    auth= FirebaseAuth.getInstance()
    //binding.idRLWallpapers.adapter= ImageAdp(arrayList)
    adapters= ImageAdp(ArrayList()) // like this
    binding.idRLWallpapers.adapter=adapters
    getUsers(null)

Turn your arrayList to local list to become empty for every 10 item.

Query.addValueEventListener(object : ValueEventListener {
      override fun onDataChange(snapshot: DataSnapshot) {
      var arrayList=ArrayList<imagemodel>() // like this

Delete below 2 lines to prevent recreate:

  //adapters= ImageAdp(arrayList) // You did this in OnCreate
  //binding.idRLWallpapers.adapter=adapters // remove these 2 lines 
  //inside Query.addValueEventListener
  adapters.addAll(arrayList)

These should work, if not i am gonna need more of your code to understand it properly.

CodePudding user response:

Declare your array list here .Then Add it to adapter

    Query.addValueEventListener(object : ValueEventListener {
                        override fun onDataChange(snapshot: DataSnapshot) {
                        var  arrayList=ArrayList<imagemodel>()
                      //your codes as it is 
                           if (snapshot.hasChildren()) {

                            for (data in snapshot.children) {

                                val image = data.child("url2").value.toString()
                                val name = data.child("voice1").value.toString()
                                val childname = 
                                data.child("childname").value.toString()
                                val id = data.child("id").value.toString()
                                val model = imagemodel(image, name, childname, id)
                                arrayList.add(model)
                            }

}

                       adapter.addAll(arrayList)
                       adapter.notyfyDataSetHasChanged()


    }
  • Related