[英]RecyclerView SearchView giving wrong URL onClick
我一直在努力在 RecyclerView 上獲得 URL 的正確 position。 我使用 FCM 獲取數據並將其存儲在 SharedPreference 上。 我在我的 RecyclerView 中實現了一個 SearchView 來過濾一個特定的單詞,它工作正常。
問題是當您搜索該單詞然后單擊recyclerView 時,它會將您發送到原始recyclerView 的第一個position 上的url,而不是過濾后的。 我試圖更改我的 BindViewHolder 上的 position,但它似乎不起作用。 Also to change the position also on my onClick function inside my ViewHolder but doesn't ether, it might be because the onClick function requires an Int and it founds what I have on Notificaciones This is the error I get and when I change it to Notificaciones在我的界面上,onClick 停止工作。
這是我的適配器
class NotifAdapter(private var mContext: Context, items:ArrayList<Notificaciones>, var listener: onClickListenerForAdapter): RecyclerView.Adapter<NotifAdapter.ViewHolder>(), Filterable {
var items: ArrayList<Notificaciones>?= null
var itemsFiltered: ArrayList<Notificaciones>?= null
init {
this.items = items
this.itemsFiltered = items
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NotifAdapter.ViewHolder {
val vista = LayoutInflater.from(mContext).inflate(R.layout.cv_notif, parent, false)
val viewHolder = ViewHolder(vista,listener)
return viewHolder
}
override fun getItemCount(): Int {
return itemsFiltered?.count()!!
}
override fun onBindViewHolder(holder: NotifAdapter.ViewHolder, position: Int) {
val item = itemsFiltered?.get(position)
holder.title?.text = item?.title
holder.body?.text = item?.body
holder.fecha?.text = item?.fecha
holder.img?.setImageResource(item?.img!!)
//Animaciones para la imagen
holder.img?.animation = AnimationUtils.loadAnimation(mContext,R.anim.aparecer_animacion)
//Animación para el mensaje
holder.mensaje?.animation = AnimationUtils.loadAnimation(mContext, R.anim.crecer_animacion)
}
inner class ViewHolder(vista:View, listener:onClickListenerForAdapter): RecyclerView.ViewHolder(vista), View.OnClickListener{
var vista = vista
var title: TextView? = null
var body: TextView? = null
var fecha: TextView? = null
var listener:onClickListenerForAdapter? = null
var img: ImageView? = null
var mensaje:RelativeLayout? = null
var items: ArrayList<Notificaciones>?= null
var itemsFiltered: ArrayList<Notificaciones>?= null
init {
title = vista.findViewById(R.id.tituloNotif)
body = vista.findViewById(R.id.textNotif)
fecha = vista.findViewById(R.id.fechaNotif)
img = vista.findViewById(R.id.imgNotif)
mensaje = vista.findViewById(R.id.contenido)
this.listener = listener
vista.setOnClickListener(this)
}
override fun onClick(v: View?) {
val adapterPosition = itemsFiltered?.get(adapterPosition)
this.listener?.onClick(v!!, adapterPosition)
}
}
override fun getFilter(): Filter {
return object : Filter() {
override fun performFiltering(constraint: CharSequence): FilterResults {
val Key = constraint.toString()
itemsFiltered = if (Key.isEmpty()) {
items
} else {
val lstFiltered = java.util.ArrayList<Notificaciones>()
for (row in items!!) {
if (row.title.toLowerCase().contains(Key.toLowerCase()) || row.body.toLowerCase().contains(Key.toLowerCase())) {
lstFiltered.add(row)
}
}
lstFiltered
}
val filterResults = FilterResults()
filterResults.values = itemsFiltered
return filterResults
}
override fun publishResults(constraint: CharSequence, results: FilterResults) {
itemsFiltered = results.values as java.util.ArrayList<Notificaciones>
notifyDataSetChanged()
}
}
}}
這是我的通知 class
class Notificaciones(title: String, body: String, fecha: String, imagen: Int){
var title = ""
var body = ""
var fecha = ""
var img = 0
init {
this.title = title
this.body = body
this.fecha = fecha
this.img = imagen
}
}
這是我的 RecyclerView 的界面
interface onClickListenerForAdapter {
fun onClick(vista: View, index: Int)
}
這是我的活動 class
class ventanaNotif : AppCompatActivity() {
val notifList = ArrayList<Notificaciones>()
var lista:RecyclerView? = null
lateinit var adaptador: NotifAdapter
var layoutManager: RecyclerView.LayoutManager? = null
lateinit var shared: SharedPreferences
private val COUNT_KEY = "Conteo"
var busqueda: EditText?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ventana_notif)
//findViews
lista = findViewById(R.id.rv_notif) //RecyclerView nuevo
busqueda = findViewById(R.id.busqueda)//Busqueda de texto
//Nuevo RecyclerView
lista?.setHasFixedSize(true)
layoutManager = LinearLayoutManager(this)
lista?.layoutManager = layoutManager
adaptador = NotifAdapter(this ,notifList, object:onClickListenerForAdapter{
override fun onClick(vista: View, index: Int) {
val url = notifList.get(index).fecha.toString()
val i = Intent(Intent.ACTION_VIEW)
i.data = Uri.parse(url)
startActivity(i)
}
})
lista?.adapter = adaptador
//funciones
notifData()
butBorrar.setOnClickListener {
shared.edit().clear().apply()
adaptador.notifyItemRemoved(adaptador.itemCount)
}
//Metodo de busqueda por TextWatcher
busqueda?.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
adaptador.filter.filter(s)
}
override fun afterTextChanged(s: Editable) {
}
})
}
override fun onBackPressed() {
val intent = Intent(this, ventanaHome::class.java)
startActivity(intent)
finish()
super.onBackPressed()
}
fun notifData() {
shared = getSharedPreferences("Notificaciones", Context.MODE_PRIVATE)
var count = shared.getInt(COUNT_KEY, 0)
for ((x, valor) in (1..count).withIndex()) {
notifList.add(
Notificaciones(
"${shared.getString("TituloData${valor}", "Todo bien")}",
"${shared.getString("CuerpoData${valor}", "No hay más notificaciones")}",
"${shared.getString("UrlData${valor}", "Sin URL ")}", R.drawable.ic_steren2)
)
adaptador.notifyItemInserted(adaptador.itemCount)
}
}}
當我改變我的界面像這樣
interface onClickListenerForAdapter {
fun onClick(vista: View, notificaciones: Notificaciones)}
我的 Activity 上的 onClick 停止工作,因為缺少從 notifList 獲取 fecha 的索引。
adaptador = NotifAdapter(this ,notifList, object:onClickListenerForAdapter{
override fun onClick(vista: View, notificaciones: Notificaciones) {
val url = notifList.get(index).fecha.toString()
val i = Intent(Intent.ACTION_VIEW)
i.data = Uri.parse(url)
startActivity(i)}
我希望你能發現錯誤。 任何幫助將不勝感激
謝謝:)
您的界面具有以下定義:
interface onClickListenerForAdapter {
fun onClick(vista: View, index: Int)
}
而且您沒有將適配器 position 作為第二個參數傳遞 - 您正在傳遞您從列表中獲得的Notificationes
object:
override fun onClick(v: View?) {
val adapterPosition = itemsFiltered?.get(adapterPosition)
this.listener?.onClick(v!!, adapterPosition)
}
因此,您需要將界面更改為:
interface onClickListenerForAdapter {
fun onClick(vista: View, notificationes: Notificaciones)
}
或將onClick
方法更改為:
override fun onClick(v: View?) {
this.listener?.onClick(v!!, adapterPosition)
}
您現在在 onClick 方法中實際擁有的內容應該像這樣命名:
override fun onClick(v: View?) {
val notificationes = itemsFiltered?.get(adapterPosition)
this.listener?.onClick(v!!, notificationes)
}
干杯!
這對我有幫助。
holder.vista.setOnClickListener{ if(url.= ""){ val i = Intent(Intent.ACTION_VIEW) i.data = Uri.parse(url) mContext.startActivity(i) } else { Toast,makeText(mContext:"No hay archivos adjuntos,)". Toast.LENGTH_SHORT).show() }}
希望這會對某人有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.