I am new in programming. I have problem with the error above for filter.
I am getting error shown below. I have tried for a few weeks but still have no idea how to solve it.
Too many arguments for public constructor adapterState(listState: List) defined in com.e.shoppingmallfooddrink.adapterState
Please help me.
Thank you very much.
Main activity
package com.e.shoppingmallfooddrink
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.SimpleAdapter
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewManager: RecyclerView.LayoutManager
private lateinit var stateSearch: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
stateSearch = findViewById(R.id.stateSearch)
viewManager = GridLayoutManager(this, 2)
viewAdapter = adapterState(stateFlag.listState)
recyclerView = findViewById<RecyclerView>(R.id.recycleViewState).apply {
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
setHasFixedSize(true)
// use a linear layout manager
layoutManager= viewManager
// specify an viewAdapter (see also next example)
adapter = viewAdapter
}
addTextListener()
}
private fun addTextListener() {
stateSearch.addTextChangedListener( object: TextWatcher{
override fun afterTextChanged(s: Editable?) {
TODO("Not yet implemented")
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
TODO("Not yet implemented")
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
var s=s.toString().toLowerCase()
data class filteredList (var name: String, var photo: String)
for (i in 0 until stateFlag.listState.size) {
val text: String = stateFlag.listState[i].name
if (text.contains(s)) {
filteredList(stateFlag.listState[i].name,stateFlag.listState[i].photo)
}
}
recyclerView.setLayoutManager(LinearLayoutManager(this@MainActivity))
viewAdapter = adapterState(filteredList, this@MainActivity)
recyclerView.setAdapter(viewAdapter)
viewAdapter.notifyDataSetChanged() // data set changed
}
}
)
}
}
Adapter
package com.e.shoppingmallfooddrink
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.grid_view_state.view.*
class adapterState (val listState:List<stateData>):
RecyclerView.Adapter<adapterState.MyViewHolder>()
{
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun setDataState(listDM: stateData){
Glide.with(itemView.context)
.load(listDM.photo)
.into(itemView.listPhotoState)
itemView.listTextState.text = listDM.name
}
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val listState = listState[position]
holder.setDataState(listState)
}
override fun getItemCount() = listState.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val textView = LayoutInflater.from(parent.context)
.inflate(R.layout.grid_view_state, parent, false)
return MyViewHolder(textView)
}
}
Data class
package com.e.shoppingmallfooddrink
data class stateData (var name: String, var photo: String)
object stateFlag {
val listState = listOf<stateData>(
stateData("Selangor","https://www.crwflags.com/fotw/images/m/my-selan.gif"),
stateData("Kuala Lumpur","https://www.crwflags.com/fotw/images/m/my-kl.gif"),
stateData("Malacca","https://www.crwflags.com/fotw/images/m/my-melak.gif"),
stateData("Johor","https://www.crwflags.com/fotw/images/m/my-01.gif"),
stateData("Penang", "https://www.crwflags.com/fotw/images/m/my-p.gif"),
stateData("Pahang","https://www.crwflags.com/fotw/images/m/my-pahan.gif"),
stateData("Kedah","https://www.crwflags.com/fotw/images/m/my-kd.gif"),
stateData("Kelantan","https://www.crwflags.com/fotw/images/m/my-kelan.gif"),
stateData("Negeri Sembilan","https://www.crwflags.com/fotw/images/m/my-neger.gif"),
stateData("Perak","https://www.crwflags.com/fotw/images/m/my-perak.gif"),
stateData("Perlis","https://www.crwflags.com/fotw/images/m/my-perli.gif"),
stateData("Sabah","https://www.crwflags.com/fotw/images/m/my-sabah.gif"),
stateData("Sarawak","https://www.crwflags.com/fotw/images/m/my-saraw.gif"),
stateData("Trengganu","https://www.crwflags.com/fotw/images/m/my-teren.gif"),
stateData("Putrajaya","https://www.crwflags.com/fotw/images/m/my-pj.gif"),
stateData("Labuan","https://www.crwflags.com/fotw/images/m/my-la.gif")
)
}
The error gives the exact issue. Too many arguments for public constructor adapterState(listState: List)
. The construtor takes only one argument
class adapterState (val listState:List<stateData>)
but you are passing two arguments:
viewAdapter = adapterState(filteredList, this@MainActivity)
Remove this@MainActivity
argument.