【发布时间】:2020-10-07 18:21:56
【问题描述】:
在我的代码中,我从我在 Recycler View 上显示的 api 获得了总共 214 个国家/地区。当我点击国旗时,我得到一个带有国家位置的 Toast(无需搜索即可获得正确的位置)。但是,当我从搜索选项中搜索任何国家,然后单击国旗时,我的位置就出错了[当国家被搜索时,搜索到的国家排在首位,它占据了那个位置]。我不知道为什么会这样,我在代码中犯了什么错误??
例如,假设有 4 个国家。在位置 0 - 印度,位置 1 - 德国,位置 2 - 巴基斯坦,位置 3 - 法国。
现在这四个国家/地区已显示在 Recycler View 上,然后印度的onClick 显示位置 0,当我单击德国时,它将显示位置 1(无需搜索)。 但是当我从列表中搜索法国并在搜索后单击法国时,它应该显示位置 3 但它不是。
我的CustomAdapter.kt 为RecyclerView。
class CustomAdapter(public val context: Context, private val countriesResponse: CountriesResponse) :
RecyclerView.Adapter<CustomAdapter.MyViewHolder>(), Filterable {
var country : ArrayList<CountriesResponseItem>
var list_country: ArrayList<CountriesResponseItem>
internal var mFilter: NewFilter
override fun getFilter(): Filter {
return mFilter
}
init {
list_country = getCountries()
country = ArrayList()
country.addAll(list_country)
mFilter = NewFilter(this@CustomAdapter)
}
fun getCountries(): ArrayList<CountriesResponseItem> {
val list_country = arrayListOf<CountriesResponseItem>()
for (list1 in countriesResponse)
list_country.add(list1)
return list_country
}
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
var image: ImageView
var cv_tv_country_name: TextView
init {
image = itemView.cv_iv_country_flag
cv_tv_country_name = itemView.cv_tv_country_name
image.setOnClickListener(this)
}
override fun onClick(v: View?) {
val intent =
Intent(v?.context, CountryWiseDataActivity::class.java).putExtra("position", position)
v?.context?.startActivity(intent)
//Toast when clicked on Item
Toast.makeText(v?.context,"Item Clicked at " + getPosition(),Toast.LENGTH_SHORT).show()
}
}
val VIEW_TYPE = 1
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
var itemView = LayoutInflater.from(context).inflate(R.layout.flag_list, parent, false)
return MyViewHolder(itemView)
}
override fun getItemCount(): Int {
return country.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
Glide.with(context).load(country[position].countryInfo.flag).into(holder.image)
holder.cv_tv_country_name.text = country[position].country
}
inner class NewFilter(var customAdapter: CustomAdapter) : Filter() { <-- Search Filter
override fun performFiltering(constraint: CharSequence?): FilterResults {
country.clear()
val results = FilterResults()
if (constraint!!.isEmpty()) {
country.addAll(list_country)
} else {
val filterPattern = constraint.toString().toLowerCase().trim() { it <= ' ' }
for (list_country1 in 0..list_country.size) {
if (list_country[list_country1].country.toLowerCase()
.contains(filterPattern)
) {
country.add(list_country[list_country1])
}
}
}
results.values = country
results.count = country.size
return results
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
notifyDataSetChanged()
}
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
}
在活动中,我为搜索过滤器做了如下操作
search_countries.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
countryAdapter.getFilter().filter(s.toString())
}
})
【问题讨论】:
标签: android android-studio kotlin android-recyclerview