gpt4 book ai didi

android - RecyclerView 在日志中显示空值但没有错误

转载 作者:行者123 更新时间:2023-11-30 05:10:25 25 4
gpt4 key购买 nike

我正在尝试使用回收器 View 显示列表,但我得到的是 null 值。我有三个类 TestTestModelTestListAdapter。下面是我的 Json 输出。我在一个地方遇到错误。

val catObj = response.getJSONObject(i)

如果我提到 i 作为 int 然后我得到一个错误,我需要输入 i.toString() 进行字符串转换.即使在将 i 转换为字符串后,我也没有得到输出,错误日志中也没有错误。我在我的代码中使用了 VolleyKotlin。任何帮助表示赞赏。

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)
}

测试列表适配器类

    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

}

最佳答案

正如我在评论中提到的——我不太熟悉 kotlin 语言,但您发布的 JSON 示例:

[{"id":"1","name":"AAAA"},{"id":"3","name":"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 .

关于android - RecyclerView 在日志中显示空值但没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53843572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com