Android Studio 模拟器上的 RecyclerView 中未显示数据

问题描述 投票:0回答:1

我正在开发一个 Android 应用程序,我试图在 RecyclerView 中显示数据。数据是从本地数据库获取的,应该显示在 RecyclerView 中,但它没有显示在模拟器中。这是我的代码:

tclass HomeActivity : BaseActivity() {

    private lateinit var binding: ActivityHomeBinding

    var arrMainCategory = ArrayList<CategoryItems>()
    var arrSubCategory = ArrayList<Recipes>()

    var mainCategoryAdapter = MainCategoryAdapter()
    var subCategoryAdapter = SubCategoryAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        enableEdgeToEdge()

        binding = ActivityHomeBinding.inflate(layoutInflater)
        setContentView(binding.root)

        getDataFromDb()

        arrSubCategory.add(Recipes(1, "Meatloaf"))
        arrSubCategory.add(Recipes(2, "Chicken and Rice"))
        arrSubCategory.add(Recipes(3, "Chocolate Cake"))
        arrSubCategory.add(Recipes(4, "Iced Tea"))

        subCategoryAdapter.setData(arrSubCategory)

        binding.rvSubCategory.layoutManager = LinearLayoutManager(this@HomeActivity,
            LinearLayoutManager.HORIZONTAL, false)
        binding.rvSubCategory.adapter = subCategoryAdapter

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private fun getDataFromDb() {
        lifecycleScope.launch {
            try {
                val cat = RecipeDatabase.getDatabase(this@HomeActivity).recipeDao().getAllCategory()
                arrMainCategory = cat.toCollection(ArrayList())
                Log.d("HomeActivity", "Data fetched from database: $arrMainCategory") // Log the fetched data
                withContext(Dispatchers.Main) {
                    mainCategoryAdapter.setData(arrMainCategory)
                    binding.rvMainCategory.layoutManager = LinearLayoutManager(this@HomeActivity,
                        LinearLayoutManager.HORIZONTAL, false)
                    binding.rvMainCategory.adapter = mainCategoryAdapter
                }
            } catch (e: Exception) {
                Log.e("HomeActivity", "Error fetching data from database", e) // Log any errors
            }
        }
    }
}ype here
class MainCategoryAdapter: RecyclerView.Adapter<MainCategoryAdapter.RecipeViewHolder>() {

    var arrMainCategory = ArrayList<CategoryItems>()
    var ctx: Context? = null
    var listener: AdapterView.OnItemClickListener? = null

    class RecipeViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val tvDishName: TextView = itemView.findViewById(R.id.tv_dish_name)
        val imgDishName: ImageView = itemView.findViewById(R.id.img_dish)
    }

    fun setData(arrData: ArrayList<CategoryItems>) {
        arrMainCategory = arrData as ArrayList<CategoryItems>
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipeViewHolder {
        return RecipeViewHolder(LayoutInflater.from(parent.context)
            .inflate(R.layout.item_rv_main_category, parent, false))
    }

    override fun getItemCount(): Int {
        return arrMainCategory.size
    }

    override fun onBindViewHolder(holder: RecipeViewHolder, position: Int) {
        holder.tvDishName.text = arrMainCategory[position].strcategory
    }
}

我已经添加了日志语句,并且数据似乎从数据库中正确获取,但它没有显示在 RecyclerView 中。这是日志:

D/HomeActivity:从数据库获取的数据:[CategoryItems(...)] D/HomeActivity:适配器中的数据集:4

我已经检查了 RecyclerView 和适配器是否已正确初始化。是什么原因导致这个问题?

kotlin android-recyclerview
1个回答
0
投票
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityHomeBinding.inflate(layoutInflater)
    setContentView(binding.root)

    binding.rvMainCategory.layoutManager = LinearLayoutManager(
        this@HomeActivity, LinearLayoutManager.HORIZONTAL, false
    )
    binding.rvMainCategory.adapter = mainCategoryAdapter

    getDataFromDb()
}
© www.soinside.com 2019 - 2024. All rights reserved.