So I created a class to change layouts according to a construcotr but it wont work?

梦想与她 提交于 2020-07-16 08:25:23

问题


SO i have to classes CollegeSearch.kt and CollegeSwitcher.kt The first one make use of a recyclerview to put up a list for people to use and select a country. When they tap on it, I want the layout to switch by switching the class. here is the CollegeSearch.kt class:

class CollegeSearch : AppCompatActivity() {

    var college_DU:MutableList<String> = ArrayList()
    var displayList:MutableList<String> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle1?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.college_search)
        loadData()
//        country_list.layoutManager = LinearLayoutManager(this)
        college_list.layoutManager = GridLayoutManager(this,1)
        college_list.adapter = CountryAdapter(displayList,this)

    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.main,menu)
        val searchItem = menu.findItem(R.id.college_search_menu)
        if(searchItem != null){
            val searchView = searchItem.actionView as SearchView
            val editext = searchView.findViewById<EditText>(androidx.appcompat.R.id.search_src_text)
            editext.hint = "Search here..."

            searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
                override fun onQueryTextSubmit(query: String?): Boolean {
                    return true
                }

                override fun onQueryTextChange(newText: String?): Boolean {

                    displayList.clear()
                    if(newText!!.isNotEmpty()){
                        val search = newText.toLowerCase()
                        college_DU.forEach {
                            if(it.toLowerCase().contains(search)){
                                displayList.add(it)
                            }
                        }
                    }else{
                        displayList.addAll(college_DU)
                    }
                    college_list.adapter?.notifyDataSetChanged()
                    return true
                }

            })
        }

        return super.onCreateOptionsMenu(menu)
    }


    class CountryAdapter(items : List<String>,ctx:Context) : RecyclerView.Adapter<CountryAdapter.ViewHolder>(){

        private var list = items
        private var context = ctx

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder?.name?.text = list[position]
            holder?.name.setOnClickListener {
                val fieldIntent = Intent()
                fieldIntent.putExtra("SelectedCountry",list[position])
                val selectedCountry = fieldIntent.getStringExtra("SelectedCountry")
                val secondActivity = Intent(this@CountryAdapter.context, CollegeSwitcher(selectedCountry)::class.java )
                context.startActivity(secondActivity)
            }
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(LayoutInflater.from(context).inflate(R.layout.country_child,parent,false))
        }


        class ViewHolder(v: View) : RecyclerView.ViewHolder(v){
            val name = v.college_name!!
        }
    }

    private fun loadData(){
        college_DU.add("St. Stephen’s College")
        college_DU.add("Shri Ram College of Commerce")
        college_DU.add("Hansraj College")
        college_DU.add("Hindu College")
        college_DU.add("Miranda House")
        college_DU.add("Lady Shri Ram College")
        college_DU.add("Kirorimal College")
        college_DU.add("Ramjas College")
        college_DU.add("Sri Venkateswara College")
        college_DU.add("Indrapastha College For Women")
        displayList.addAll(college_DU)
    }
}

If you look at the CountryAdapter you can see in the onBinderView how i use it to change the layouts by using the latter class CollegeSwitcher.kt class:

class CollegeSwitcher : AppCompatActivity {
    constructor(name: String) : super() {
        this.collegeName = name
    }

    val collegeName: String
    var collegeCall =  ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        for (letter in collegeName) {
            if (letter.isWhitespace()) {
                collegeCall+="_"
            }
            else{
                collegeCall+=letter.toLowerCase()
            }
        }
        val searchIdCollege: String=collegeCall
        val resID = resources.getIdentifier(collegeCall, "layout", packageName)
        setContentView(resID)
    }
}

So i want to know why then, when the user clicks on the name on the list, does the app crash? And when i tried debugging it by running it, both the constructor "name" and "collegeName" are empty. I tried running the for loop in a separate kotlin terminal and what i found out was that for any input that i give in the for loop it returns the xml names perfectly fine. So any help guys?

New Update: class CollegeSwitcher : AppCompatActivity() {

var collegeCall =  ""
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val collegeName: String = intent.getStringExtra("SelectedCountry")
    for (letter in collegeName) {
        if (letter.isWhitespace()) {
            collegeCall+="_"
        }
        else{
            collegeCall+=letter.toLowerCase()
        }
    }
    val searchIdCollege: String=collegeCall
    val resID = resources.getIdentifier(collegeCall, "layout", packageName)
    setContentView(resID)
}

} now gives me this error java.lang.IllegalStateException: intent.getStringExtra("SelectedCountry") must not be null.

来源:https://stackoverflow.com/questions/62606142/so-i-created-a-class-to-change-layouts-according-to-a-construcotr-but-it-wont-wo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!