我认为主要问题是在将foodInfo添加到foodLists以及将数据传递到listAdapter的过程中发生的。我确实用Log.d检查了foodData.foodName,它工作正常,但是当我尝试使用Log.d获取foodList时,它不起作用。
这是我目前的代码
已解决
我的适配器
class listAdapter(val food : ArrayList<Foods>) : RecyclerView.Adapter<listAdapter.ViewHolder>() {
val unfoldedIndexes = HashSet<Int>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.cell, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return food.count()
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(position)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(position: Int) = with(itemView) {
// folding_cell.setBackgroundColor(resources.getColor(R.color.colorPrimary))
folding_cell.backSideColor = resources.getColor(R.color.White)
if (unfoldedIndexes.contains(position)) {
cell_title_view.visibility = View.GONE
cell_content_view.visibility = View.VISIBLE
} else {
cell_content_view.visibility = View.GONE
cell_title_view.visibility = View.VISIBLE
}
itemView.setOnClickListener {
// toggle clicked cell state
folding_cell.toggle(false)
// register in adapter that state for selected cell is toggled
registerToggle(position)
}
}
private fun registerToggle(position: Int) {
if (unfoldedIndexes.contains(position))
registerFold(position)
else
registerUnfold(position)
}
private fun registerFold(position: Int) {
unfoldedIndexes.remove(position)
}
private fun registerUnfold(position: Int) {
unfoldedIndexes.add(position)
}
}
}
我的recyclerView活动
class FoodListActivity : BaseActivity(1) {
private val TAG = "FoodListActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_food_list)
setupBottomNavigation()
Log.d(TAG,"onCreate")
getFromFirebase()
}
private fun getFromFirebase() {
val currentUserUID = FirebaseAuth.getInstance().uid.toString()
val ref = FirebaseDatabase.getInstance().reference
ref.child("users").child(currentUserUID).child("foods").addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
setHasFixedSize(true)
recyclerview.layoutManager = LinearLayoutManager(this@FoodListActivity)
p0.children.forEach {
Log.d("getFood", it.toString())
val foodData = it.getValue(Foods::class.java)
if (foodData != null) {
val foodList = ArrayList<Foods>()
val adapter = ListAdapter(foodList)
foodList.add(foodData)
recyclerview.adapter = adapter
}
}
}
})
}}