Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

android - Create Search Filter for ListView error 'T' parameter

I'm 8 days into Kotlin and I'm trying to create a search filter for a ListView that displays a set of internships. After following a tutorial, when I try to create the onQueryTextSubmit function, it says that there is an error with the 'T' parameter and the value should be mentioned in input types. Due to this, I can not call the filter method either. For reference, I did a Udemy course from which I copied the listview to create my list. It's based off of a custom 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

        }

    }
}
question from:https://stackoverflow.com/questions/65931664/create-search-filter-for-listview-error-t-parameter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your list is Internship type but you are searching for string type that's why its giving you error.

make sure you passing Internship type data while searching for any field:

 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")
            }

        })
    }

like giving above


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...