从工具栏中搜索自定义listView-Kotlin

时间:2019-06-11 02:21:38

标签: android listview kotlin

我在工具栏中书写时需要过滤列表视图。

CustomList:其具有3个textView的布局。 FullList:它是一个带有ListView的活动

我从SQLitle表填充FullList

FullList.kt:

override fun onCreate(savedInstanceState: Bundle?) {
        ...
        setSupportActionBar(toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)

        var listView = findViewById<ListView>(R.id.listView)
        val customAdptor = CustomAdapterListView(this)
        listView.adapter=customAdptor
    }

class CustomAdapterBJCP(private val context: Activity): BaseAdapter(){

    var db = DataBaseHandler(context)
    var data = db.DataExample()

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        val inflater = context.layoutInflater
        val view1 = inflater.inflate(R.layout.CustomList,null)
        var fName= view1.findViewById<TextView>(R.id.txtOne)
        var FDetail= view1.findViewById<TextView>(R.id.txtTwo)
        var fCount = view1.findViewById<TextView>(R.id.txtThree)

        fName.text = data[p0].name
        FDetail.text = data[p0].detail
        fCount .text= data[p0].count
        return view1
    }

    override fun getItem(p0: Int): Any {
        return data.get(p0).name
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {
        return data.size
    }


    }

搜索listView我实现了一个菜单项:

<menu...>
<item android:id="@+id/action_search"
          android:title="Search"
          app:showAsAction="always|collapseActionView"
          android:icon="@android:drawable/ic_menu_search"
          app:actionViewClass="android.support.v7.widget.SearchView"  />
</menu>

由于这个原因,我决定在FullList.kt中创建此函数:

...
 override fun onCreateOptionsMenu(menu: Menu): Boolean {

        menuInflater.inflate(R.menu.menu_main,menu)
        val searchItem = menu.findItem(R.id.action_search)
        if(searchItem!=null){
            val searchView=searchItem.actionView as SearchView
            searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
                override fun onQueryTextSubmit(query: String?): Boolean {
                    return true
                }
                override fun onQueryTextChange(newText: String?): Boolean {
                    return true
                }

            })
        }
        return super.onCreateOptionsMenu(menu)
    }
...

现在我需要在搜索工具栏中输入一个单词,然后在ListView中找到该项目

我如何在Kotlin中做到这一点?

对不起,如果我的英语不好,我来自秘鲁。

提前THX。

1 个答案:

答案 0 :(得分:3)

我希望这对您有用

Create()方法的活动中,从本地数据库而不是在适配器中获取数据。

所以onCreate()方法是:

 override fun onCreate(savedInstanceState: Bundle?) {
            ...
            setSupportActionBar(toolbar)
            supportActionBar!!.setDisplayHomeAsUpEnabled(true)

            var db = DataBaseHandler(context)
            var data = db.DataExample()  //this method must be return interms of your pojo class type.

            var listView = findViewById<ListView>(R.id.listView)
            val customAdptor = CustomAdapterBJCP(this,data)
            listView.adapter=customAdptor
        }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {

            menuInflater.inflate(R.menu.menu_main,menu)
            val searchItem = menu.findItem(R.id.action_search)
            if(searchItem!=null){
                val searchView=searchItem.actionView as SearchView
               searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
                override fun onQueryTextSubmit(query: String?): Boolean {
                    return true
                }

                override fun onQueryTextChange(newText: String?): Boolean {
                    if (newText != null) {
                   /* var dataList : List<Your Pojo Class> = list.filter { it.name.startsWith(newText) }
                         .sortedBy { it.name }*/

                      var dataList: MutableList<Your Pojo Class> = data.filter { it.name.startsWith(newText) }.toMutableList()  // data is list (i.e you get data from db)

                      if (dataList.size.equals(0)) {
                        Toast.makeText(this@MainActivity, "No Record Found", Toast.LENGTH_SHORT).show()   
                      }

                        for (item in dataList) {
                            System.out.println("Searched Data: " + item.name)
                        }

                       customAdptor = CustomAdapterBJCP(dataList , this@Your Activity)
                       listView.adapter = customAdptor 
                       customAdptor?.notifyDataSetChanged

                    }
                    return true

                }

            })
            }
            return super.onCreateOptionsMenu(menu)
        }

修改适配器

class CustomAdapterBJCP(private val context: Context, var data: MutableList<Your Pojo Class>)BaseAdapter(){

  //  var db = DataBaseHandler(context)
    //var data = db.DataExample()

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        val inflater = context.layoutInflater
        val view1 = inflater.inflate(R.layout.CustomList,null)
        var fName= view1.findViewById<TextView>(R.id.txtOne)
        var FDetail= view1.findViewById<TextView>(R.id.txtTwo)
        var fCount = view1.findViewById<TextView>(R.id.txtThree)

        fName.text = data.get(p0).name
        FDetail.text = data.get(p0).detail
        fCount .text= data.get(p0).count
        return view1
    }

    override fun getItem(p0: Int): Any {
        return data.get(p0).name
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {
        return data.count()
    }
}