使用改造的搜索视图 android

searchview using retrofit android

我指的是 this tutorial。 一切正常,没有错误,但它 没有过滤 当我输入任何内容时。它没有过滤列表。

我不知道我做错了什么。

以下是我的适配器和activity代码。

适配器代码

class Table_Adapter(val context: Context) : RecyclerView.Adapter<Table_Adapter.MyViewHolder>(),Filterable {
    var mFilteredList: MutableList<Tabledata> = mutableListOf()
    var Tablelist: MutableList<Tabledata> = mutableListOf()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

        val view = LayoutInflater.from(parent.context).inflate(
                R.layout.table_recycle_item,
                parent,
                false
        )
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int {
        return Tablelist.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

        holder.productname.text = Tablelist.get(position).name
        holder.producername.text = Tablelist.get(position).producer

        holder.productprice.text = Tablelist.get(position).cost.toString()

        Glide.with(context).load(Tablelist.get(position).product_images)
                .into(holder.image)
        holder.rate.setRating(Tablelist.get(position).rating.toFloat());
        holder.itemView!!.setOnClickListener {
            val context:Context=holder.itemView.context
            val i=Intent(
                    context,
                    Product_details::class.java
            )
            i.putExtra("id", Tablelist.get(position).id.toString())
            i.putExtra("image", Tablelist.get(position).product_images)
            context.startActivity(i)
        }
    }

    fun setMovieListItems(movieList: MutableList<Tabledata>){
        this.Tablelist = movieList;
        notifyDataSetChanged()
    }

    class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {

        val productname: TextView = itemView!!.findViewById(R.id.title)
        val producername: TextView = itemView!!.findViewById(R.id.title1)
        val productprice: TextView = itemView!!.findViewById(R.id.title2)
        val rate: RatingBar=itemView!!.findViewById(R.id.ratingbar)
        val image: ImageView = itemView!!.findViewById(R.id.image)
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            override fun performFiltering(charSequence: CharSequence): FilterResults? {
                val charString = charSequence.toString()
                if (charString.isEmpty()) {
                    mFilteredList = Tablelist
                } else {
                    val filteredList: MutableList<Tabledata> = ArrayList()
                    for (androidVersion in Tablelist) {
                        /* if (androidVersion.name.toLowerCase()
                                 .contains(charString)
                         ) {
                            filteredList.add(androidVersion)
                         }*/
                        if (androidVersion.name.toLowerCase()
                                        .contains(charString.toLowerCase()) || androidVersion.name
                                        .contains(charSequence)
                        ) {
                            filteredList.add(androidVersion)
                        }
                    }
                    mFilteredList = filteredList
                }
                val filterResults = FilterResults()
                filterResults.values = mFilteredList
                return filterResults
            }

            override fun publishResults(charSequence: CharSequence?, filterResults: FilterResults) {
                mFilteredList = filterResults.values as MutableList<Tabledata>
                notifyDataSetChanged()
            }
        }
    }
}

Activity:

class Tables : AppCompatActivity() {
    lateinit var recyclerView: RecyclerView
    lateinit var recyclerAdapter: Table_Adapter
    var Tablelist : MutableList<Tabledata> = mutableListOf()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.table_activity)
        var mActionBarToolbar = findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbartable);
        setSupportActionBar(mActionBarToolbar);
        if (getSupportActionBar() != null){
            getSupportActionBar()?.setDisplayHomeAsUpEnabled(true);
            getSupportActionBar()?.setDisplayShowHomeEnabled(true);
            getSupportActionBar()?.setHomeAsUpIndicator(R.drawable.ic_keyboard_arrow_left_black_24dp);
            getSupportActionBar()?.setTitle((Html.fromHtml("<font color=\"#FFFFFF\">" + getString(R.string.Tables) + "</font>")));
        }
        recyclerView = findViewById(R.id.recyleview)
        recyclerAdapter = Table_Adapter(this)
        recyleview.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(
                DividerItemDecoration(
                        recyclerView.context,
                        DividerItemDecoration.VERTICAL
                )
        )
        recyleview.adapter = recyclerAdapter
        RetrofitClient.instancetable.getAllPhotos(product_category_id = "1", value = 1).enqueue(
                object : Callback<Table_response> {
                    override fun onFailure(call: Call<Table_response>, t: Throwable) {
                        Toast.makeText(applicationContext, "falied", Toast.LENGTH_LONG).show()
                    }
                    override fun onResponse(
                            call: Call<Table_response>,
                            response: Response<Table_response>
                    ) {

                        if (response?.body() != null) {

                            recyclerAdapter.setMovieListItems((response.body()?.data as MutableList<Tabledata>?)!!)
                        }
                    }

                })
    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            android.R.id.home -> {
                NavUtils.navigateUpFromSameTask(this)
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        val search: MenuItem = menu.findItem(R.id.search)
        val searchView: SearchView = MenuItemCompat.getActionView(search) as SearchView
        search(searchView)
        return true
    }
    override fun onBackPressed() {
        super.onBackPressed()
    }
    private fun search(searchView: SearchView) {
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String): Boolean {
                recyclerAdapter.getFilter().filter(newText)
                return true
            }
        })
    }
}

提前致谢。

搜索视图查询不会产生过滤列表,因为您没有使用过滤列表

onBindViewHoldergetItemCount 仅使用 Tablelist 变量。 mFilteredList 仅保留过滤后的结果 - 不使用它们。

按照您当前的方式存储原始列表,但在 onBindViewHoldergetItemCount 中将 Tablelist 替换为 mFilteredList。此外,需要 setMovieListItems 中的小更新。 mFilteredList 也被声明为不可变的。 performFiltering 也有所改进,并没有变短。一切呈现如下:

class Table_Adapter() : RecyclerView.Adapter<Table_Adapter.MyViewHolder>(), Filterable {

    var mFilteredList: List<Tabledata> = listOf()
    var tableList: MutableList<Tabledata> = mutableListOf()

    ...

    override fun getItemCount(): Int {
        return mFilteredList.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val tableItem = mFilteredList.get(position)

        holder.productname.text = tableItem.name
        holder.producername.text = tableItem.producer

        holder.productprice.text = tableItem.cost.toString()

        Glide.with(holder.itemView!!.context).load(tableItem.product_images)
                .into(holder.image)
        
        holder.rate.setRating(tableItem.rating.toFloat());
        holder.itemView!!.setOnClickListener {
            val context: Context = holder.itemView.context
            val i = Intent(
                    context,
                    Product_details::class.java
            )
            i.putExtra("id", tableItem.id.toString())
            i.putExtra("image", tableItem.product_images)
            context.startActivity(i)
        }
    }

    fun setMovieListItems(movieList: MutableList<Tabledata>) {
        tableList = movieList.toMutableList() // makes a copy
        mFilteredList = movieList
        notifyDataSetChanged()
    }

    ...

    override fun getFilter(): Filter {
        return object : Filter() {
            override fun performFiltering(charSequence: CharSequence): FilterResults? {
                val queryString = charSequence.toString()

                val filterResults = FilterResults()
                filterResults.values =
                        if (queryString.isEmpty()) {
                            tableList
                        } else {
                            tableList.filter {
                                it.name.contains(queryString, ignoreCase = true) || it.name.contains(charSequence)
                            }
                        }
                return filterResults
            }

            override fun publishResults(charSequence: CharSequence?, filterResults: FilterResults) {
                mFilteredList = filterResults.values
                notifyDataSetChanged()
            }
        }
    }
}