简体   繁体   English

如何从适配器类更改 TextView 文本?

[英]How can I change TextView text from adapter class?

I need to change text according to my click listener function.我需要根据我的click listener功能更改文本。 I used findViewById method to get textview and I use setText to change text of textview but in here I get an error which is nullexception .我使用findViewById方法获取textview并使用setText更改textview文本,但在这里我收到一个错误,即nullexception I am quite new at Kotlin with no Java experience.我是 Kotlin 的新手,没有 Java 经验。 I did research about that I saw Interface method to change text I tried it but it wasn't work.我做了研究,我看到了改变文本的Interface method ,我试过了,但没有用。 How can I solve it?我该如何解决?

Here my code which is from Adapter Class:这是我来自适配器类的代码:

var dd = holder.customView.findViewById<TextView>(R.id.toplamSayi)!!
dd.setText(tt)

Also I tried dd.text = tt but it wasn't work also.我也试过dd.text = tt但它也不起作用。

Edit:编辑:

My Adapter Class:我的适配器类:

private class DetailPageAdapterForSepetPage: RecyclerView.Adapter<DetailPageViewHolderForSepetPage>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailPageViewHolderForSepetPage {
        val layoutInflater = LayoutInflater.from(parent?.context)
        val customDetailView = layoutInflater.inflate(R.layout.sepet_sayfasi_list_row_detail, parent,false)
        return DetailPageViewHolderForSepetPage(customDetailView)
    }

    override fun onBindViewHolder(holder: DetailPageViewHolderForSepetPage, position: Int) {
        var urunDb = sepetArr.get(position)

        holder.customView.urun_text.text = urunDb.eklenenUrun
        holder.customView.fiyat_text.text = urunDb.eklenenFiyat
        holder.customView.adet_text.text = "x" + urunDb.eklenenAdet

        var addedCount = urunDb.eklenenAdet.toInt()

        // ekleme butonu fonksiyonu
        holder.customView.imageButton4.setOnClickListener {
            addedCount += 1
            holder.customView.adet_text.text = "x$addedCount"

            // BURADA COUNT U BİR ARTTIRACAGIZ VE ARKADAN DIGER İKİLİ (URUN VE FIYAT) TUTAN ARRAYE DE EKLEME YAPACAGIZ.. YADA CIKARMASINI ASAGIDA..

            var urun = holder.customView.urun_text.text
            var fiyat = holder.customView.fiyat_text.text
            var adet = holder.customView.adet_text.text
            var addedData = SepeteGidenUrunlerModel(
                urunEklenen = urun.toString(),
                fiyatEklenen = fiyat.toString()
            )

            var fiyatStr = fiyat.toString()
            var droped = fiyatStr.dropLast(2)
            var doubleEklenecek = droped.toDouble()

            toplamTutar += doubleEklenecek
            var tt = toplamTutar.toString()

            holder.customView.toplamSayi.setText(tt)
            FirstDetailActivity.sepeteEklenenUrunler.add(addedData)
        }

        // cikarma butonu fonksiyonu
        holder.customView.imageButton5.setOnClickListener {

            if (addedCount > 1) {
                addedCount -= 1
                holder.customView.adet_text.text = "x$addedCount"

                var urun = holder.customView.urun_text.text
                var fiyat = holder.customView.fiyat_text.text

                var cikarilan = SepeteGidenUrunlerModel(
                    urunEklenen = urun.toString(),
                    fiyatEklenen = fiyat.toString()
                )

                FirstDetailActivity.sepeteEklenenUrunler.remove(cikarilan)

            }else if (addedCount == 1) {

                var urun = holder.customView.urun_text.text
                var fiyat = holder.customView.fiyat_text.text
                var adet = holder.customView.adet_text.text
                var cikarilan = SepeteGidenUrunlerModel(
                    urunEklenen = urun.toString(),
                    fiyatEklenen = fiyat.toString()
                )

                FirstDetailActivity.sepeteEklenenUrunler.remove(cikarilan)

                var sepetteData = SepetModel(
                    eklenenUrun = urun.toString(),
                    eklenenFiyat = fiyat.toString(),
                    eklenenAdet = adet.toString()
                )

                sepetArr.removeAt(position)
                notifyDataSetChanged()
            }
        }
    }

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

private class DetailPageViewHolderForSepetPage(val customView: View): RecyclerView.ViewHolder(customView){
}

And XML :和 XML :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SepetSayfasi">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/sepet_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/toplamText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="24dp"
        android:text="TextView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/sepet_recycler_view" />

    <TextView
        android:id="@+id/topalmTutar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/toplamText"
        app:layout_constraintTop_toTopOf="@+id/toplamText" />

    <TextView
        android:id="@+id/textToplam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="TextView"
        app:layout_constraintStart_toStartOf="@+id/toplamText"
        app:layout_constraintTop_toBottomOf="@+id/toplamText" />

    <TextView
        android:id="@+id/toplamSayi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textToplam"
        app:layout_constraintTop_toTopOf="@+id/textToplam" />
</androidx.constraintlayout.widget.ConstraintLayout>
  1. You need to pass to your adapter the list of elements that are gonna populate the view您需要将要填充视图的元素列表传递给您的适配器

  2. You need to declare the views of your RecyclerView layout in your ViewHolder , then you can access it in onBindViewHolder您需要在ViewHolder声明RecyclerView布局的ViewHolder ,然后您可以在onBindViewHolder访问它

Try to use this code as the base of your Adapter and complete it as needed尝试使用此代码作为您的 Adapter 的基础并根据需要完成它

private class DetailPageAdapterForSepetPage(
    private val activity: <the-class-of-your-activity>,
    private val sepetArr: List<the-type-of-your-list>
) : RecyclerView.Adapter<DetailPageViewHolderForSepetPage>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailPageViewHolderForSepetPage {
        val customDetailView = LayoutInflater.from(parent.context).inflate(R.layout.sepet_sayfasi_list_row_detail, parent, false)
        return DetailPageViewHolderForSepetPage(customDetailView)
    }

    override fun onBindViewHolder(holder: DetailPageViewHolderForSepetPage, position: Int) 
    {
        val urunDb = sepetArr[position]

        holder.btn.setOnClickListener {
            activity.updateText("Your text")
        }
        
        // -- Put the rest of your initializazion code here --
    }

    override fun getItemCount(): Int = sepetArr.size

    inner class DetailPageViewHolderForSepetPage(customView: View): RecyclerView.ViewHolder(customView) {
        val btn: Button = customView.findViewById(<your-button-id>)

        // -- Put the other views in your recycler view item here --
    }
}

In your parent activity declare a public method在您的父活动中声明一个公共方法

updateText(text: String) {
    // Update the text view
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 recyclerview 适配器类更改主要活动中 textview 的文本 - How change text of textview inside main activity from recyclerview adapter class 如何从其他功能更改Textview文本? - How can I change Text of Textview from other funcion? 我如何从活动中更改片段的textview文本 - how can i change textview text of fragment from activity 如何更改 Adapter 类中的 itemLayout - How can i change the itemLayout in Adapter class 我如何在这里更改textview文本 - How can I change textview text here 无法在分页器适配器+ FragmentPagerAdapter中的事件上更改TextView文本 - Can't change TextView text on event in pager adapter + FragmentPagerAdapter 如何更改带有特定文本的textview上的文本颜色? - How can i change the text color on textview with with a specific text? 如何从适配器更改文本视图的颜色,textView 的背景在 drawable 中定义 - How to change the color of text view from adapter, textView's background is defined in drawable 如何从另一个Activity更改LinerLayout扩展类中TextView的文本? - How to change text of a TextView in a LinerLayout extended class from another Activity? 如何从扩展 RelativeLayout 的 class 更改 Activity 中的 Textview 文本 - How to change Textview text in Activity from class that extends RelativeLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM