我进入Kotlin已经8天了,我正在尝试为显示一组实习经历的ListView创建一个搜索过滤器。学习完教程后,当我尝试创建onQueryTextSubmit函数时,系统提示'T‘参数有错误,应该在输入类型中提及该值。因此,我也不能调用filter方法。作为参考,我做了一个Udemy课程,我复制了其中的listview来创建我的列表。它基于自定义的baseAdapter。
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
}
}
}发布于 2021-01-28 15:55:08
你的列表是实习生类型,但你正在搜索字符串类型,这就是为什么它会给你错误的原因。
确保在搜索任何字段时传递实习生类型数据:
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")
}
})
}就像上面给的一样
https://stackoverflow.com/questions/65931664
复制相似问题