RecyclerView数据插入异常:弹窗提交后无内容显示
RecyclerView添加数据后无内容显示问题
我编写了一段代码,用户可通过弹窗输入三项内容,提交后应将数据插入RecyclerView的三列布局中。目前弹窗功能正常,但确认提交后,包含RecyclerView的折叠布局展开却无内容显示。
核心代码
// Initialize the RecyclerView itemList = mutableListOf() itemAdapter = ItemAdapter(itemList) binding.dailyRecycler.layoutManager = GridLayoutManager(this, 3) binding.dailyRecycler.adapter = itemAdapter binding.dayAdd.setOnClickListener { showPopup() } } private fun showPopup() { val dialogView = LayoutInflater.from(this).inflate(R.layout.popup_calculate, null) val builder = AlertDialog.Builder(this) .setView(dialogView) .setTitle("Add Item") .setPositiveButton("Add") { dialog, which -> val itemName = dialogView.findViewById<EditText>(R.id.editTextItem).text.toString() val day = dialogView.findViewById<EditText>(R.id.editTextDay).text.toString().toInt() val price = dialogView.findViewById<EditText>(R.id.editTextPrice).text.toString().toInt() val newItem = Item(itemName, day, price) itemList.add(newItem) itemAdapter.notifyDataSetChanged() } .setNegativeButton("Cancel") { dialog, which -> dialog.dismiss() } builder.show() } }
RecyclerView适配器代码
// RecyclerView adapter class ItemAdapter(private val itemList: MutableList<Item>) : RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() { // ViewHolder class inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { private val itemNameTextView: TextView = itemView.findViewById(R.id.itemNameTextView) private val dayTextView: TextView = itemView.findViewById(R.id.dayTextView) private val priceTextView: TextView = itemView.findViewById(R.id.priceTextView) fun bind(item: Item) { itemNameTextView.text = item.itemName dayTextView.text = item.day.toString() priceTextView.text = item.price.toString() } } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder { val itemView = LayoutInflater.from(parent.context).inflate(R.layout.calculate_items, parent, false) return ItemViewHolder(itemView) } override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { val currentItem = itemList[position] holder.bind(currentItem) } override fun getItemCount() = itemList.size }
布局文件calculate_items.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp" > <TextView android:id="@+id/itemNameTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Item Name" android:textSize="16sp" android:padding="8dp" android:textColor="@color/white" /> <TextView android:id="@+id/dayTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Day" android:textSize="16sp" android:padding="8dp"/> <TextView android:id="@+id/priceTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Price" android:textSize="16sp" android:padding="8dp"/> </LinearLayout>
内容的提问来源于stack exchange,提问作者LIM DOU LIANG CALVIN
相关产品推荐
相关产品推荐

