使用Kotlin从RecyclerAdapter更改MainActvity中的TextView [英] Change TextView in MainActvity from RecyclerAdapter using Kotlin

查看:178
本文介绍了使用Kotlin从RecyclerAdapter更改MainActvity中的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to change the text in a TextView (id: dailyTotalTextView) in MainActivity, when I click a button in a RecyclerView. But I get an error saying dailyTotalTextView must not be null.

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

I have a basic knowledge of Kotlin and no Java experience, so I might be going all wrong about this.

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

The TextView displays the sum of values in a list. The list items contain a number and a button to delete the item. When I delete an item I want the TextView to change the total.

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

In MainActivity an item can be added to the list and by using .notifyDataSetChanged() I can set the text to the new value.

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

The DrinksToday object holds the variables and methods for changing the list.

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

The adapter holds the onClick for the delete button, so it can delete the item at the clicked position. This is also where I wanted to change the TextView.

MainActivity:

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对象:

DrinksToday object:

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

编辑:我更改了内部类Holder,现在使用kotlinx.android.synthetic访问视图引用.

I changed the inner class Holder and now use kotlinx.android.synthetic to access the view references.

RecyclerAdapter:

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) {
        fun bindDrinks(drinks: Drinks, context: Context) {
            itemView.drinkListText.text = drinks.volume
            itemView.drinkListButtonDelete.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 XML:

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

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

When I run this code I get an error saying:

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.我希望这能奏效.希望有人可以帮助我.

I don't understand why this TextView is Null. I would expect this to work. Hopefully someone can help me out.

推荐答案

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

I got it figured out, thanks to the reply of Lalit Behera and some research.

我将此接口添加到了RecylerAdapter:

I added this interface to my RecylerAdapter:

interface OnItemClickListener {
    fun onItemClick(dailyTotal: Int)
}

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

Changed the constructor of the RecyclerAdapter by adding an OnItemClickListener:

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

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

And to the setOnClickListenr in the class Holder I added:

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

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

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

Then in MainActivity my adapter became:

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

这篇关于使用Kotlin从RecyclerAdapter更改MainActvity中的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆