如何使用Kotlin将过滤器添加到RecyclerViewAdapter?

时间:2018-10-13 23:08:44

标签: android android-studio filter android-recyclerview kotlin

我想为此RecyclerViewAdapter添加一个过滤器,请帮助提供此代码

  

注意:我使用一个班级来填写清单,这个班级需要四个   参数为INPUTS(三个字符串和一个图像(整数))。

import android.content.Intent
import android.support.v7.widget.CardView
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import kotlin.collections.ArrayList


@Suppress("UNREACHABLE_CODE")
class RecyclerViewAdapter(private val mContext: List_of_Books, private val mData: MutableList<Book>) : RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>() {
    private var mFilteredList: MutableList<Book>? = null


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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

        val view: View
        val mInflater = LayoutInflater.from(mContext)
        view = mInflater.inflate(R.layout.cardveiw_item_book, parent, false)
        return MyViewHolder(view)
    }

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

        holder.tv_book_title.text = mData[position].getTitle()
        holder.img_book_thumbnail.setImageResource(mData[position].thumbnail)
        holder.cardView.setOnClickListener {
            val intent = Intent(mContext, Book_Activity::class.java)

            // passing data to the book activity
            intent.putExtra("Title", mData[position].getTitle())
            intent.putExtra("Description", mData[position].description)
            intent.putExtra("Thumbnail", mData[position].thumbnail)
            intent.putExtra("Category", mData[position].category)


            // start the activity
            mContext.startActivity(intent)
        }
    }

    fun setfilter(listitem: MutableList<Book>): MutableList<Book>? {
    mFilteredList!!.clear()
    /*mFilteredList = ArrayList()*/
    mFilteredList!!.addAll(listitem)
    notifyDataSetChanged()
    return mFilteredList
}


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

        internal var tv_book_title: TextView = itemView.findViewById(R.id.book_title_id) as TextView
        internal var img_book_thumbnail: ImageView = itemView.findViewById(R.id.book_img_id) as ImageView
        internal var cardView: CardView = itemView.findViewById(R.id.cardview_id) as CardView

    }


}

RecyclerView适配器

    fun filter(pl: MutableList<Book>, query: String): MutableList<Book> {
        var query = query
        query = query.toLowerCase()
        val filteredModeList = ArrayList<Book>()
        for (model in pl) {
            val text = model.getTitle()!!.toLowerCase()
            if (text.startsWith(query) || (text.contains(query))) {
                filteredModeList.add(model)
            }
        }
        return filteredModeList
    }
    val editText = findViewById<EditText>(R.id.editText)
    editText.hint = getString(R.string.Search_here)

    editText.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(p0: Editable?) {
           val filtermodelist = filter(booksList, p0.toString())
            myAdapter.setfilter(filtermodelist)

        }

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

        }
    })

当我用EdiText编写内容时,什么都没有改变,我在Editext上使用以下代码:

import com.medanis.fneclisbooks.R.attr.title
import com.medanis.fneclisbooks.R.string.category
import com.medanis.fneclisbooks.R.string.description
import java.util.ArrayList
import kotlin.reflect.KMutableProperty1
import android.R.attr.name



class Book(title: String, category: String, description: String, var thumbnail: Int) {

    private var title: String? = title
    var category: String? = category
    var description: String? = description

    fun getTitle(): String? {
        return title
    }

    fun setTitle(title: String) {
        this.title = title
    }}

书本班

  File "/Users/Beadog/PycharmProjects/M08_12.3/gameATMMachine.py", line 82, in getBalance
    return format(self.__balance, ".2f")
AttributeError: 'int' object has no attribute '_Account__balance'
  

我认为问题出在适配器的过滤器上   错误的请帮助我修复该问题

请帮助我,如果您需要更多信息,请在评论中告诉我。

0 个答案:

没有答案