我正在尝试使用回收者视图显示列表,但我得到了null
的价值。我有三个班级Test
,TestModel
和TestListAdapter
。以下是我的Json输出。我在一个地方遇到错误。
val catObj = response.getJSONObject(i)
。
如果我将i
称为int
,那么我得到一个错误,因此,我需要输入i.toString()
进行字符串转换。即使将i
转换为字符串后,我也没有得到输出,并且错误日志中没有错误。我在代码中使用Volley
和Kotlin
。任何帮助表示赞赏。
Error:--Type Mismatch
val catObj = response.getJSONObject(i)
Json输出:
[{"id":"1","name":"AAAA"},{"id":"3","name":"BBBB"}]
测试类:
class Test : AppCompatActivity() {
var volleyRequest:RequestQueue?=null
var TestList: ArrayList<TestModel>?=null
var adapter:TestListAdapter?=null
var layoutManager: RecyclerView.LayoutManager?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_Test)
var HelloLink="https://www.abc.app"
TestList= ArrayList<TestModel>()
volleyRequest = Volley.newRequestQueue(this)
getTest(HelloLink)
}
fun getTest(url: String) {
print("Url Value is:"+url)
val catRequest = JsonObjectRequest(Request.Method.GET,
url, Response.Listener {
response: JSONObject ->
try {
for (i in 0..response.length() - 1) {
val Test=TestModel()
val catObj = response.getJSONObject(i)
var name = catObj.getString("name")
Test.name = name
Log.d("Name Result==>>", name)
TestList!!.add(Test)
adapter = TestListAdapter(TestList!!, this)
layoutManager = LinearLayoutManager(this)
recyclerViewTest.layoutManager = layoutManager
recyclerViewTest.adapter = adapter
}
adapter!!.notifyDataSetChanged()
}catch (e: JSONException) { e.printStackTrace()}
},
Response.ErrorListener {
error: VolleyError? ->
try {
Log.d("Error:", error.toString())
}catch (e: JSONException){e.printStackTrace()}
})
volleyRequest!!.add(catRequest)
}
}
TestListAdapter类
class TestListAdapter(private val list:ArrayList<TestModel>,
private val context: Context):RecyclerView.Adapter<TestListAdapter.ViewHolder>()
{
override fun getItemCount(): Int {
return list.size
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
val view
=LayoutInflater.from(context).inflate(R.layout.list_Test,p0,false)
return ViewHolder(view)
}
override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
p0?.bindItem(list[p1])
}
class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
fun bindItem(Test:TestModel)
{
var name:TextView=itemView.findViewById<TextView>(R.id.CatName)
name.text=Test.name
}
}
}
模型类
class TestModel
{
var id:Int?=null
var name:String?=null
}
答案 0 :(得分:0)
将数据添加到适配器时,您会遇到一些问题,
如下更新适配器
class TestListAdapter( private val context: Context):RecyclerView.Adapter<TestListAdapter.ViewHolder>()
private val list = arrayListOf<TestModel>
public fun injectData(list : ArrayList<TestModel>){
this.list.addAll(list)
notifyDataSetChanged()
}
{
override fun getItemCount(): Int {
return list.size
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
val view
=LayoutInflater.from(context).inflate(R.layout.list_Test,p0,false)
return ViewHolder(view)
}
override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
p0?.bindItem(list[p1])
}
class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
fun bindItem(Test:TestModel)
{
var name:TextView=itemView.findViewById<TextView>(R.id.CatName)
name.text=Test.name
}
}
}
并在您的TestClass
中:
adapter = TestListAdapter(this)
layoutManager = LinearLayoutManager(this)
recyclerViewTest.layoutManager = layoutManager
recyclerViewTest.adapter = adapter
try {
for (i in 0..response.length() - 1) {
val Test=TestModel()
val catObj = response.getJSONObject(i)
var name = catObj.getString("name")
Test.name = name
Log.d("Name Result==>>", name)
TestList!!.add(Test)
}
adapter.injectData(TestList);
}catch (e: JSONException) { e.printStackTrace()}
还有一件小事,请考虑在使用!!
时,使用前应检查null
示例,而不是
TestList!!.add(Test)
尝试
TestList?.let {
it.add(Test)
}
答案 1 :(得分:0)
正如我在评论中提到的那样-我不太熟悉kotlin语言,但是您发布的JSON示例:
[{“ id”:“ 1”,“名称”:“ AAAA”},{“ id”:“ 3”,“名称”:“ BBBB”}]
实际上是JSONArray
而不是JSONObject
,所以我认为您应该使用如下代码:
fun getTest(url: String) {
print("Url Value is:"+url)
val catRequest = JsonArrayRequest(Request.Method.GET,
url, Response.Listener {
response: JSONArray ->
try {
for (i in 0..response.length() - 1) {
val Test=TestModel()
val catObj = response.getJSONObject(i)
var name = catObj.getString("name")
Test.name = name
Log.d("Name Result==>>", name)
TestList!!.add(Test)
adapter = TestListAdapter(TestList!!, this)
layoutManager = LinearLayoutManager(this)
recyclerViewTest.layoutManager = layoutManager
recyclerViewTest.adapter = adapter
}
adapter!!.notifyDataSetChanged()
}catch (e: JSONException) { e.printStackTrace()}
},
Response.ErrorListener {
error: VolleyError? ->
try {
Log.d("Error:", error.toString())
}catch (e: JSONException){e.printStackTrace()}
})
volleyRequest!!.add(catRequest)
}
请注意,JsonArrayRequest
代替了JsonObjectRequest
,响应是JSONArray
而不是JSONObject
。