错误(public 构造函数 adapterState(listState List,stateData>) 没有伴随对象和太多参数
Error( Does not have companion object and too many arguments for public constructor adapterState(listState List,stateData>)
我是编程新手。我对过滤器的上述错误有疑问。
我收到如下所示的错误。我已经试了几个星期了,还是不知道怎么解决。
Too many arguments for public constructor adapterState(listState: List) defined in com.e.shoppingmallfooddrink.adapterState
请帮帮我。
非常感谢。
主要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
}
}
)
}
}
适配器
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)
}
}
数据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")
)
}
错误给出了确切的问题。 Too many arguments for public constructor adapterState(listState: List)
。构造函数只接受一个参数
class adapterState (val listState:List<stateData>)
但是你传递了两个参数:
viewAdapter = adapterState(filteredList, this@MainActivity)
删除 this@MainActivity
参数。
我是编程新手。我对过滤器的上述错误有疑问。
我收到如下所示的错误。我已经试了几个星期了,还是不知道怎么解决。
Too many arguments for public constructor adapterState(listState: List) defined in com.e.shoppingmallfooddrink.adapterState
请帮帮我。
非常感谢。
主要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
}
}
)
}
}
适配器
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)
}
}
数据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")
)
}
错误给出了确切的问题。 Too many arguments for public constructor adapterState(listState: List)
。构造函数只接受一个参数
class adapterState (val listState:List<stateData>)
但是你传递了两个参数:
viewAdapter = adapterState(filteredList, this@MainActivity)
删除 this@MainActivity
参数。