0

I think the main problem happened during adding foodInfo into foodLists, and also the part where I pass data to listAdapter. I did check with Log.d for foodData.foodName and it works fine, but when I try Log.d to get the foodList it doesn't work.

here are my code for the moment

EDITED SOLVED

My Adapter

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)
    }
}

}

My Activity for the 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

                }
            }

        }

    })
}}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    In which place of your activity are you checking the list and you say it doesn't work. I don't see any log statement regarding that. – Alex Mamo Dec 15 '18 at 07:35
  • @AlexMamo I just added the log part – Darren Chuah Dec 15 '18 at 09:54
  • Your `foodList` list is empty or it does contain only one item? – Alex Mamo Dec 15 '18 at 11:00
  • @AlexMamo I couldn't get anything in them, I think my way of adding stuff into ArrayList have some problem, I'm kinda new in Kotlin. If my approach of adding stuff into ArrayList is wrong please correct me. – Darren Chuah Dec 15 '18 at 11:35
  • When I do Log.d("getFood", it.toString()) I can get the key and the children(which is the foodName, foodPrice and foodDesc). but I'm sure that none of the data had been pass to ListAdapter because the getItemCount() get 0 – Darren Chuah Dec 15 '18 at 11:39
  • Please add your database structure and the code where you are setting the adapter. – Alex Mamo Dec 15 '18 at 11:48
  • Please add your database structure (as a screenshot) and the code where you are setting the adapter. – Alex Mamo Dec 15 '18 at 12:00
  • SOLVED [https://stackoverflow.com/questions/49404719/recyclerview-is-not-showing-data-from-firebase-kotlin?rq=1] – Darren Chuah Dec 15 '18 at 12:34

0 Answers0