大家好,我在Recyclerview中使用gson api,但适配器出现此错误,并显示
myAdapter'类型的表达式'myAdapter'不能作为函数调用。找不到功能“调用”
并且当我运行没有数据的代码时,它给了我这个错误:
lateinit属性myAdapter尚未初始化
我的活动和课程
private val datalist: MutableList<Sura> = mutableListOf()
private lateinit var myAdapter: MyAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quran_a_p_i)
myAdapter = myAdapter(datalist) // here is the error
apiQuranRecyclerView.layoutManager = LinearLayoutManager(this)
apiQuranRecyclerView.addItemDecoration(DividerItemDecoration(this, OrientationHelper.VERTICAL))
apiQuranRecyclerView.adapter = myAdapter
AndroidNetworking.initialize(this)
AndroidNetworking.get("http://api.islamhouse.com/v1/mykey/quran/get-category/462298/ar/json/")
.build()
.getAsObject(Surasnames::class.java, object : ParsedRequestListener<Surasnames>{
override fun onResponse(response: Surasnames) {
datalist.addAll(response.suras)
myAdapter.notifyDataSetChanged()
}
override fun onError(anError: ANError?) {
}
})
}
我的适配器
class MyAdapter (private val datalist: MutableList<Sura>): RecyclerView.Adapter<myHolder>() {
private lateinit var context: Context
override fun onCreateViewHolder(parent: ViewGroup, p1: Int): myHolder {
context = parent.context
return myHolder(LayoutInflater.from(context).inflate(R.layout.api_quran_view, parent, false))
}
override fun getItemCount(): Int = datalist.size
override fun onBindViewHolder(holder: myHolder, position: Int) {
val data = datalist[position]
val apisuraname = holder.itemView.apiSuraNames
val surasnames = "${data.id} ${data.title}"
apisuraname.text = surasnames
holder.itemView.setOnClickListener {
Toast.makeText(context, surasnames, Toast.LENGTH_SHORT).show()
}
}
我的持有人
class myHolder(itemView:View):RecyclerView.ViewHolder(itemView){}
我的模特
data class Sura(
@SerializedName("add_date")
val addDate: Int,
@SerializedName("api_url")
val apiUrl: String,
@SerializedName("id")
val id: Int,
@SerializedName("title")
val title: String,
@SerializedName("type")
val type: String
)
第二个
data class Surasnames(
@SerializedName("add_date")
val addDate: Int,
@SerializedName("description")
val description: String,
@SerializedName("id")
val id: Int,
@SerializedName("locales")
val locales: List<Any>,
@SerializedName("suras")
val suras: List<Sura>,
@SerializedName("title")
val title: String)
预先感谢
myAdapter = myAdapter(datalist)
to
myAdapter = MyAdapter(datalist)