Home > Software design >  Incorrect data in recycleview
Incorrect data in recycleview

Time:12-20

I am using RecycleView in android studio. When data is coming from Firebase it is correct at first. Later, when I add 1 sms to the list of type List(), I get wrong data as a result. For example: If List is {1,2,3,4,5}, Add 6 to the list on the screen The elements of List {1,2,3,4,5,6} should appear in this order. In this case, After the chase List {1,2,3,4,5,1,2,3,4,5,6} is like this. After 1 update it works fine.

class ChatAdapter(var list:MutableList<Sms>,var context: Context):RecyclerView.Adapter<RecyclerView.ViewHolder>(){
    companion object {
        private const val ITEM_RECEIVE = 1
        private const val ITEM_SENT = 2
    }

    private val shared by lazy {
        SharedPref(context)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == 1) {
            val view = LayoutInflater.from(context).inflate(R.layout.item_he_sms, parent, false)
            ReceiveVewHolder(view)
        }else{
            val view = LayoutInflater.from(context).inflate(R.layout.item_my_sms, parent, false)
            SentViewHolder(view)
        }
    }

    override fun getItemViewType(position: Int): Int {
        val currentMessage = list[position]
        return if (currentMessage.emailAddress==shared.getEmail()){
            ITEM_SENT
        }else{
            ITEM_RECEIVE
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (shared.getEmail()==list[position].emailAddress){
            holder as SentViewHolder
            holder.textView.text = list[position].smsText
        }else{
            holder as ReceiveVewHolder
            holder.textView.text = list[position].smsText
            if (list[position].gender=="Male"){
                holder.imagePerson.setImageResource(R.drawable.male_avatar)
            }else{
                holder.imagePerson.setImageResource(R.drawable.female_avatar)
            }
        }
    }

    override fun getItemCount(): Int =list.size

    inner class SentViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val textView: TextView = view.findViewById(R.id.my_message)
    }

    inner class ReceiveVewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val textView: TextView = view.findViewById(R.id.he_message)
        val imagePerson:CircleImageView=view.findViewById(R.id.imagePerson)
    }
}

CodePudding user response:

Create this methode inside your Adapter :

    fun updateList(list: List<Sms>){
       this.list.clear()
       this.list.addAll(list)
       notifyDataSetChanged()
    }

then call it from inside your observer method, it should resolve your problem.

CodePudding user response:

Xato Firebase'dan getAllSms da, sendSms() usuli ishlayotganda, {1,2,3,4,5,1,2,3,4,5,6} ro'yxat sifatida qaytariladi. result.invoke ni bajarishdan oldin onDataChange-da list.clear ni bajarishingiz kerak.

override fun getAllSms(result: (UiState<ArrayList<Sms>>) -> Unit) {
    CoroutineScope(Dispatchers.IO).launch {
        val list = ArrayList<Sms>()
        myRef.getReference("global")
                .addValueEventListener(object :ValueEventListener{
                    override fun onDataChange(snapshot: DataSnapshot) {
                        list.clear()
                        snapshot.children.forEach {
                            val sms:Sms=it.getValue(Sms::class.java)!!
                            list.add(sms)
                        }
                        list.forEach {
                            Log.d("LLIISSTT", it.smsText)
                        }
                        result.invoke(UiState.Success(list))
                    }

                    override fun onCancelled(error: DatabaseError) {
                        result.invoke(UiState.Failure(error.message))
                    }
                })
    }
}
  • Related