2

Whenever I check the first item in the checklist, it checks some other items as well.

This is my onBindViewHolder method in the adapter class

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val itemText = reminderItemSet[position].item.toString()

    holder.checkBox.isChecked = reminderItemSet[position].completion == true

    if(holder.checkBox.isChecked){
        holder.textView.apply{paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
        text = itemText}
    } else
        holder.textView.apply{paintFlags = paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
        text = itemText}

    holder.checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
        mActivity.toggleCheck(reminderItemSet[position].item.toString(), isChecked)
    }

    if (reminderItemSet[position].ttid != "null") {
        holder.playIV.visibility = View.VISIBLE
        holder.infoIV.setOnLongClickListener {
            mActivity.startStreaming(reminderItemSet[position])
        }
    } else {
        holder.playIV.visibility = View.GONE
    }


    holder.infoIV.setOnClickListener {
        mActivity.infoDialog(reminderItemSet[position])
    }


    holder.textView.setOnLongClickListener { 
        mActivity.removePendingitem(reminderItemSet[position].item.toString()) 
    }
}

This is the method in main activity that loads the data from firebase

fun getTopLevelItems(): ArrayList<reminderItem> {
    database.addValueEventListener(object : ValueEventListener {
        @SuppressLint("NotifyDataSetChanged")
        override fun onDataChange(dataSnapshot: DataSnapshot) {
            itemList.clear()
            for (i in dataSnapshot.children) {
                val item = i.child("item").value.toString()
                val owner = i.child("owner").value.toString()
                val completion = i.child("completion").value.toString().toBoolean()
                val date = i.child("date").value.toString()
                val ttid = i.child("ttid").value.toString()
                itemList.add(reminderItem(item, owner, completion, date, ttid))
            }
            mAdapter.notifyDataSetChanged()
        }
        override fun onCancelled(error: DatabaseError) {
            Log.w("TAG", "Failed to read value.", error.toException())
        }
    })
    return itemList
}

This is the method that appends the checks on firebase

fun toggleCheck(itemName: String, isChecked: Boolean) {
    Log.i("TAGGER", "toggleCheck: $itemName to $isChecked")
    database.child(itemName).child("completion").setValue(isChecked)
}

And here is the log for toggle check when I check "10 things I hate about you"

2022-09-03 14:44:06.035 19979-19979/com.example.vindication I/TAGGER: toggleCheck: 10 things I hate about you to true
2022-09-03 14:44:06.115 19979-19979/com.example.vindication I/TAGGER: toggleCheck: Shyam Singha Roy  to true
2022-09-03 14:44:06.348 19979-19979/com.example.vindication I/TAGGER: toggleCheck: Shyam Singha Roy  to true
2022-09-03 14:44:06.363 19979-19979/com.example.vindication I/TAGGER: toggleCheck: The Proposal  to true
2022-09-03 14:44:06.491 19979-19979/com.example.vindication I/TAGGER: toggleCheck: The Proposal  to true
2022-09-03 14:44:06.516 19979-19979/com.example.vindication I/TAGGER: toggleCheck: 我想你 to true
2022-09-03 14:44:06.621 19979-19979/com.example.vindication I/TAGGER: toggleCheck: 我想你 to true

Here's a link to the code

Here's a video showing the bug

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
TheWiseNerd
  • 21
  • 1
  • 4
  • There is no way you can return the `itemList` object as a result of a method. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Sep 04 '22 at 06:32

1 Answers1

1

When your an item data of the list updated, maybe change your code looks like it:

holder.checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
    mActivity.toggleCheck(reminderItemSet[position].item.toString(), isChecked)
    notifyItemChanged(position)
} 

看起来似乎是你更新了数据,但是 adapter 对应的视图没有做相应的更新,改成这样试试呢?

holder.checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
    mActivity.toggleCheck(reminderItemSet[position].item.toString(), isChecked)
    notifyItemChanged(position)
} 
nanck
  • 26
  • 3