[英]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>
You need to pass to your adapter the list of elements that are gonna populate the view您需要将要填充视图的元素列表传递给您的适配器
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.