please I'm trying to dynamically add a textitem via an EditText by clicking a button in a Recyclerview, but when I click the button to add the text in the Array related to the Adapter and I apply the adapter.notifyDataSetCh.anged() the recycleview does not update and no longer detects click
mainActivity.kt
ackage com.example.recycleviewwork
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.EditText
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class Travel : AppCompatActivity(), View.OnClickListener {
var check_travel_list = arrayOf<String>("cni", "psp", "hhn", "ddp", "ppe", "leh")
var state_check_bool = arrayOf<Boolean>(false, false, false, false,false,false)
val adapter = travel_list_adapter(check_travel_list,state_check_bool, this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_travel)
var textEditValue = ""
// RECYCLEVIEW Travel page
val recview_travel = findViewById<RecyclerView>(R.id.travel_recview)
recview_travel.layoutManager = LinearLayoutManager(this)
recview_travel.adapter = adapter
//EDIT text
val edit_text_view = findViewById<EditText>(R.id.textedit)
// ADD travel button
val button_add_travel = findViewById<Button>(R.id.button_travel)
button_add_travel.setOnClickListener()
{
textEditValue = edit_text_view.text.toString()
check_travel_list += textEditValue
state_check_bool += false
adapter.notifyDataSetChanged()
println(textEditValue)
}
}
override fun onClick(vue: View) {
val index = vue.tag as Int
//changement des bool de couleur
if(index != null)
{
state_check_bool[index] = !state_check_bool[index]
adapter.notifyDataSetChanged()
}
}
}
Adapter.kt
package com.example.recycleviewwork
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView
class travel_list_adapter(val travel_check_list : Array <String>,val travel_state_check : Array<Boolean> , val travel_click : View.OnClickListener) : RecyclerView.Adapter<travel_list_adapter.ViewHolder>()
{
class ViewHolder (itemview: View ): RecyclerView.ViewHolder(itemview)
{
val cartview = itemview.findViewById<CardView>(R.id.cartview_travel_list)
val text_check = itemview.findViewById<TextView>(R.id.text_travel_list)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): travel_list_adapter.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view_item = inflater.inflate(R.layout.travel_list,parent,false)
return ViewHolder(view_item)
}
override fun getItemCount(): Int {
return travel_check_list.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val current_check = travel_check_list[position]
val state_state_check = travel_state_check[position]
holder.cartview.setOnClickListener(travel_click)
holder.cartview.tag = position
holder.text_check.text = current_check
if(state_state_check == true)
{
holder.text_check.setTextColor(Color.parseColor("#8BC34A"))
}
else
{
holder.text_check.setTextColor(Color.parseColor("#000000"))
}
}
}
However, by changing the value of a text in the Array linked to Adapter without changing the array lenght , the recycleview displays the change normally.