首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在自定义listView中搜索,从工具栏- Kotlin

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

Stack Overflow用户
提问于 2019-06-11 10:21:38
回答 1查看 390关注 0票数 0

当我在工具栏中写东西的时候,我需要过滤我的列表视图。

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

我从SQLite表填充FullList

FullList.kt:

代码语言:javascript
复制
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,我实现了一个菜单项:

代码语言:javascript
复制
<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中创建这个函数:

代码语言:javascript
复制
...
 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中做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-11 14:05:32

我希望这对你有用。

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

所以onCreate()方法是:

代码语言:javascript
复制
 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)
        }

修改适配器

代码语言:javascript
复制
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()
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56535633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档