I dont know what to do in this case because every 7 rows the image drawable is favorite even when it shouldn't be.
class RecipesActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recipes)
getRecipes()
}
companion object{
val REC_KEY = "REC_KEY"
}
var adapter = GroupAdapter<GroupieViewHolder>()
private fun getRecipes(){
val ref = FirebaseDatabase.getInstance().getReference("/recipes")
val adapter = GroupAdapter<GroupieViewHolder>()
ref.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
p0.children.forEach{
val reci = it.getValue(Recipes::class.java)
if(reci != null) {
adapter.add(RecipesItem(reci))
}
}
adapter.setOnItemClickListener{ item, view ->
val recItem = item as RecipesItem
val intent = Intent(view.context, RecipeDetail::class.java)
intent.putExtra(REC_KEY, recItem.recipes)
startActivity(intent)
}
recycler_recipes.adapter = adapter
}
override fun onCancelled(p0: DatabaseError) {
}
})
}
}
This class is to put the items in the views. I need them to be, but when I check for favourite or not there's the problem.
class RecipesItem(val recipes: Recipes): Item<GroupieViewHolder>(){
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.itemView.name_recycler.text = recipes.reci_name
viewHolder.itemView.type_recipes.text = recipes.reci_diet
viewHolder.itemView.timeToPrepare.text = recipes.reci_time.plus(" min")
//checkiffav
val user = FirebaseAuth.getInstance().currentUser!!.uid
val fav = FirebaseDatabase.getInstance().getReference("/users/$user/fav_recipes/${recipes.id}")
fav.addListenerForSingleValueEvent(object: ValueEventListener{
override fun onDataChange(p0: DataSnapshot){
if(p0.exists()){
viewHolder.itemView.favorite.setImageResource(R.drawable.favorite)
}
}
override fun onCancelled(p0: DatabaseError) {
}
})
}
override fun getLayout(): Int {
return R.layout.row_recipes
}
}
Basically I have 20 recipes and I have a favorite system, but the recycler view only gets the id of the recipes on every 7 rows, then it repeats the same id's! I'm really new to kotlin and firebase and I dont know how to solve. On every 7 rows the recipe appears has fav because the id that the firebase reference is getting is the same as the first one.
在 Groupie 问题跟踪器上回答: https : //github.com/lisawray/groupie/issues/320#issuecomment-581915693
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.