gpt4 book ai didi

java - EditText 没有过滤 Android Kotlin 中的 RecyclerView 列表

转载 作者:行者123 更新时间:2023-12-01 18:12:48 25 4
gpt4 key购买 nike

我正在尝试通过 EditText 过滤 RecyclerView 中的列表。在 Activity 中,在适配器对象中传递响应,该对象从 ViewModel 类获取。并且在adapter中,写了filter方法。总的来说,EditText 的搜索过滤器不起作用。下面写了代码,我不知道如何解决这个问题。

项目有一个 Activity、Adapter、XML 文件和 ViewModel 类。

fragment_availabletender

此页面有 EditText 和 RecyclerView。

         <EditText
android:id="@+id/editMobileNo"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="0dp"
android:background="@drawable/login_edittext"
android:ems="10"
android:hint="Search Tender"
android:gravity="center"
android:drawableLeft="@drawable/search_3_24"
android:layout_below="@+id/tendertext"
android:paddingLeft="50dp"
>
</EditText>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/tender_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:color="@color/cardview_light_background"
android:padding="6dp"
android:background="@null"
android:scrollbars="vertical"
android:layout_marginTop="10dp"
android:layout_below="@+id/text1"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
/>

模型类这个类有模型

   data class TenderProperty(

val id: String,
@Json(name = "img_src") val imgSrcUrl: String,
val type: String,
val price: Double
)

View 模型这里得到了回应

class NotificationViewModel : ViewModel() {


val response: LiveData<List<TenderProperty>>
get() = _response

private var _responseByArray = ArrayList<TenderProperty>()
val responseByArray: List<TenderProperty>
get() = _responseByArray

private val _navigateToSelectedProperty = MutableLiveData<TenderProperty>()
val navigateToSelectedProperty: LiveData<TenderProperty>
get() = _navigateToSelectedProperty

// Create a Coroutine scope using a job to be able to cancel when needed
private var viewModelJob = Job()

private val coroutineScope = CoroutineScope(viewModelJob + Dispatchers.Main )


init {
getTenderRealEstateProperties()
}

private fun getTenderRealEstateProperties() {

coroutineScope.launch {
// Get the Deferred object for our Retrofit request
var getPropertiesDeferred = TenderApi.retrofitService.getProperties()
try {
// this will run on a thread managed by Retrofit
val listResult = getPropertiesDeferred.await()

_responseByArray= listResult as ArrayList<TenderProperty>

Log.d("respbyary",responseByArray.toString())

} catch (e: Exception) {

_response.value = ArrayList()
Log.d("class",responseByArray.toString())
}
}
}

这是适配器类

class ListTenderAdapter(context: Context, var tender: List<TenderProperty>) : ListAdapter<TenderProperty, ListTenderAdapter.TenderPropertyViewHolder>(DiffCallback),Filterable {

var originalData=tender

var filteredData = ArrayList<TenderProperty>()

//dought22
class TenderPropertyViewHolder(private var binding: TenderListViewBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(tenderProperty: TenderProperty) {
binding.property = tenderProperty
binding.executePendingBindings()
}
}

companion object DiffCallback : DiffUtil.ItemCallback<TenderProperty>()
{
override fun areItemsTheSame(oldItem: TenderProperty, newItem: TenderProperty): Boolean {
return oldItem === newItem
}

override fun areContentsTheSame(oldItem: TenderProperty, newItem: TenderProperty): Boolean {
return oldItem.id == newItem.id
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TenderPropertyViewHolder {
return TenderPropertyViewHolder(TenderListViewBinding.inflate(LayoutInflater.from(parent.context)))
}

override fun onBindViewHolder(holder: TenderPropertyViewHolder, position: Int) {
val tenderProperty = getItem(position)
holder.bind(tenderProperty)
}

override fun getFilter(): Filter {

return object : Filter() {

override fun performFiltering(constraint: CharSequence?): FilterResults {

var filteredList = ArrayList<TenderProperty>()
if (constraint.isNullOrBlank()) {
filteredData.addAll(originalData)
}
else {
for (data in originalData) {
if (
data.id.toLowerCase().contains(constraint.toString().toLowerCase())||
data.price.toString().toLowerCase().contains(constraint.toString().toLowerCase())||
data.type.toLowerCase().contains(constraint.toString().toLowerCase()))
{
filteredList.add(data)
} }
}
val filterResults = FilterResults()
filterResults.values = filteredList
return filterResults
}
override fun publishResults(constraint: CharSequence, results: FilterResults) {

filteredData.clear()
filteredData.addAll(results.values as ArrayList<TenderProperty>)
notifyDataSetChanged()

}
}}}

这是 Activity

 class TenderListActivity:AppCompatActivity() {

lateinit var adapter: ListTenderAdapter
lateinit var search:EditText
private lateinit var binding: FragmentAvailabletenderBinding
private val viewModel: NotificationViewModel by lazy {
ViewModelProviders.of(this).get(NotificationViewModel::class.java)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_availabletender)

binding = DataBindingUtil.setContentView<FragmentAvailabletenderBinding>(
this,
R.layout.fragment_availabletender
)

search = binding.editMobileNo

val divider = DividerItemDecoration(applicationContext, DividerItemDecoration.VERTICAL)
binding.tenderList.addItemDecoration(divider)

setupRecyclerView()
initViewModel()
search()
backButton()
}

private fun setupRecyclerView() {

binding.tenderList.addItemDecoration(MarginItemDecoration(5))
adapter = ListTenderAdapter(this, viewModel.responseByArray)
binding.tenderList.setAdapter(adapter)

}

private fun initViewModel() {
viewModel.response.observe(this, Observer {
adapter.submitList(it)
})

}

private fun backButton()
{
binding.backbtn.setOnClickListener(View.OnClickListener {

val backbtn: Intent = Intent(this,MainActivity::class.java)
startActivity(backbtn)

})
}

private fun search()
{
search.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) {
adapter.getFilter().filter(s.toString())

}

override fun afterTextChanged(s: Editable) {

}
})
}}

最佳答案

我没有在适配器中使用过滤器,但我认为你可以使用 list.filter 并通过 DiffUtil 设置 NewData 吗?尽管这可能不是最好的解决方案

关于java - EditText 没有过滤 Android Kotlin 中的 RecyclerView 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60433747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com