使用Kotlin从RecyclerAdapter更改MainActvity中的TextView

时间:2019-06-01 10:42:21

标签: android kotlin textview recycler-adapter

当我单击RecyclerView中的按钮时,我想在MainActivity中更改TextView(id:dailyTotalTextView)中的文本。但是我收到一条错误消息,指出dailyTotalTextView不能为空。

我具有Kotlin的基本知识,并且没有Java经验,因此我可能对此完全错了。

TextView在列表中显示值的总和。列表项目包含一个数字和一个删除项目的按钮。删除项目时,我希望TextView更改总数。

在MainActivity中,可以将一个项目添加到列表中,并且可以使用.notifyDataSetChanged()将文本设置为新值。

DrinksToday对象保存用于更改列表的变量和方法。

适配器按住onClick的删除按钮,因此它可以在单击位置删除该项目。这也是我想更改TextView的地方。

MainActivity:

class MainActivity : AppCompatActivity() {

    private lateinit var adapter: TodayDrinksRecyclerAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mainTextDailyTotal.text = dailyTotal.toString()

        adapter = TodayDrinksRecyclerAdapter(this, DrinksToday.drinksTodayList)
        drinksTodayList.adapter = adapter

        val layoutManager = LinearLayoutManager(this)
        drinksTodayList?.layoutManager = layoutManager
    }

    fun addWaterClick(@Suppress("UNUSED_PARAMETER") view: View) {
        DrinksToday.addDrink()
        adapter.notifyDataSetChanged()
        mainTextDailyTotal.text = dailyTotal.toString()
    }
}

DrinksToday对象:

object DrinksToday {

    var currentGlass = Drinks("water01", "250", "ml")
    var dailyTotal = 0
    var drinksTodayList: MutableList<Drinks> = mutableListOf()

    fun addDrink() {
        dailyTotal += currentGlass.volume.toInt()
        drinksTodayList.add(0, currentGlass)
    }

    fun removeDrink(position: Int) {
        drinksTodayList.removeAt(position)
    }
}

RecyclerAdapter:

class TodayDrinksRecyclerAdapter(private val context: Context, private val todaysDrinks: MutableList<Drinks>) :
    RecyclerView.Adapter<TodayDrinksRecyclerAdapter.Holder>() {

// ... standard adapter code here: onBindViewHolder, getItemCount, onCreateViewHolder

    open inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val drinkVolume = itemView.findViewById<TextView>(R.id.drinkListText)
        private val deleteButton = itemView.findViewById<ImageButton>(R.id.drinkListButtonDelete)

        fun bindDrinks(drinks: Drinks, context: Context) {
            drinkVolume.text = drinks.volume

            deleteButton.setOnClickListener {
                val position = adapterPosition
                DrinksToday.removeDrink(position)
                DrinksToday.dailyTotal -= drinks.volume.toInt()
                notifyDataSetChanged()

                val dailyTotalTextView = itemView.findViewById<TextView>(R.id.mainTextDailyTotal)
                dailyTotalTextView.text = DrinksToday.dailyTotal.toString()
            }
        }
    }
}

TextView XML:

<TextView
            android:id="@+id/mainTextDailyTotal"
            android:text="0"
            android:layout_width="0dp"
            android:layout_height="0dp"/>

运行此代码时,我收到一条错误消息:

java.lang.IllegalStateException: dailyTotalTextView must not be null at adapters.TodayDrinksRecyclerAdapter$Holder$bindDrinks$1.onClick(TodayDrinksRecyclerAdapter.kt:54)

这是这行代码:

val dailyTotalTextView = itemView.findViewById<TextView>(R.id.mainTextDailyTotal)

我不明白为什么这个TextView为Null。我希望这能奏效。希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

感谢Lalit Behera的回复和一些研究,我明白了。

我将此接口添加到了RecylerAdapter中:

interface OnItemClickListener {
    fun onItemClick(dailyTotal: Int)
}

通过添加OnItemClickListener来更改RecyclerAdapter的构造函数:

class TodayDrinksRecyclerAdapter(private val context: Context, private val todayDrinks: MutableList<Drinks>,
                                 private var onItemClickListener: OnItemClickListener)

在我添加的类Holder中的setOnClickListenr上:

itemView.drinkListButtonDelete.setOnClickListener {
    val position = adapterPosition
    DrinksToday.removeDrink(position)
    dailyTotal -= drinks.volume.toInt()
    notifyDataSetChanged()

    // added this line
    onItemClickListener.onItemClick(dailyTotal)
}

然后在MainActivity中,我的适配器变为:

adapter = TodayDrinksRecyclerAdapter(this, DrinksToday.drinksTodayList, object : TodayDrinksRecyclerAdapter.OnItemClickListener {
            override fun onItemClick(dailyTotal: Int) {
                mainTextDailyTotal.text = dailyTotal.toString()
            }
        })