首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为ListView错误'T‘参数创建搜索筛选器

为ListView错误'T‘参数创建搜索筛选器
EN

Stack Overflow用户
提问于 2021-01-28 13:48:54
回答 1查看 22关注 0票数 1

我进入Kotlin已经8天了,我正在尝试为显示一组实习经历的ListView创建一个搜索过滤器。学习完教程后,当我尝试创建onQueryTextSubmit函数时,系统提示'T‘参数有错误,应该在输入类型中提及该值。因此,我也不能调用filter方法。作为参考,我做了一个Udemy课程,我复制了其中的listview来创建我的列表。它基于自定义的baseAdapter。

代码语言:javascript
复制
 class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        var listofInternships = ArrayList<Internship>()
        var adapter:InternshipAdapter? = null
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //load internships
        listofInternships.add(
            Internship(R.drawable.mslogo, "Microsoft", "Marketing", "Grades 9-10",
                                "Gurgaon", "At Microsoft, we don't just train students; we build leaders",
                                "21st January", "2 Months", "10th November"))
        listofInternships.add(
            Internship(R.drawable.applelogo, "Apple", "Sales", "All Grades",
            "Mumbai", "Work at the largest smartphone manufacturer in the world",
            "8th October", "6 Months", "8th September"))
        listofInternships.add(
            Internship(R.drawable.googlelogo, "Google", "Web Design", "Grades 10-12",
            "Hyderabad", "Assist in using and integrating firebase into web apps",
            "8th July", "4 Months", "7th December"))
        listofInternships.add(
            Internship(R.drawable.uberlogo, "Uber", "Finance", "Grades 10-11",
            "Bangalore", "Ride with the mob, Alhamdulillah, Check you and me, then do your job",
            "17th September", "2 weeks", "10th September"))


        adapter = InternshipAdapter(this, listofInternships)
        lvListViewInternships.adapter = adapter

        //set onCLickListener
        
        /*lvListViewInternships.setOnItemClickListener { adapterView, view, i, l ->
            if (i>=0){
                if (view.expandableLayout.visibility == View.GONE) View.VISIBLE
                else if (view.expandableLayout.visibility == View.VISIBLE) View.GONE
            }
        }*/

        val search = findViewById<SearchView>(R.id.searchView)

        search.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(p0: String?): Boolean {
                search.clearFocus()
                if(listofInternships.contains(p0)) {

                }
            }

            override fun onQueryTextChange(p0: String?): Boolean {
                TODO("Not yet implemented")
            }

        })
    }

    class InternshipAdapter:BaseAdapter {
        var listofInternships = ArrayList<Internship>()
        var context:Context?=null
        constructor(context:Context, listofInternships:ArrayList<Internship>):super() {
            this.listofInternships = listofInternships
            this.context = context
        }

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

        override fun getItem(index: Int): Any {
            return listofInternships[index]
        }

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

        override fun getView(index: Int, p1: View?, p2: ViewGroup?): View {
            val internship = listofInternships[index]
            var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            var myView = inflator.inflate(R.layout.internship_ticket, null)
            myView.ivLogo.setImageResource(internship.logo!!)
            myView.tvTitle.text=internship.title!!
            myView.tvCategory.text=internship.category!!
            myView.tvGrades.text=internship.grades!!
            myView.tvLocation.text=internship.location!!
            myView.tvTagline.text=internship.tagline!!
            myView.btnStartDateValue.text=internship.startdate!!
            myView.btnDurationValue.text=internship.duration!!
            myView.btnApplyByValue.text=internship.applyby!!
            myView.ivLogo.setOnClickListener {
                val intent = Intent(context, ExpandableDescription::class.java)
                intent.putExtra("logo", internship.logo!!)
                intent.putExtra("title", internship.title!!)
                intent.putExtra("category", internship.category!!)
                intent.putExtra("grades", internship.grades!!)
                intent.putExtra("location", internship.location!!)
                intent.putExtra("tagline", internship.tagline!!)
                intent.putExtra("startdate", internship.startdate!!)
                intent.putExtra("duration", internship.duration!!)
                intent.putExtra("applyby", internship.applyby!!)
                context!!.startActivity(intent)

            }

            return myView

        }

    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-01-28 15:55:08

你的列表是实习生类型,但你正在搜索字符串类型,这就是为什么它会给你错误的原因。

确保在搜索任何字段时传递实习生类型数据:

代码语言:javascript
复制
 search.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(p0: String?): Boolean {
                search.clearFocus()
                if(listofInternships.contains(Internship("$p0")) {

                }
            }

            override fun onQueryTextChange(p0: String?): Boolean {
                TODO("Not yet implemented")
            }

        })
    }

就像上面给的一样

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65931664

复制
相关文章

相似问题

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