RecyclerView 在日志中显示空值但没有错误
RecyclerView is displaying Null Value but No Error in the log
我正在尝试使用回收器视图显示列表,但我得到的是 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:
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
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
class TestModel
{
var id:Int?=null
var name:String?=null
}
向适配器添加数据时遇到问题,
如下更新您的适配器
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)
}
正如我在评论中提到的——我对 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
。
我正在尝试使用回收器视图显示列表,但我得到的是 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:
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
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
class TestModel
{
var id:Int?=null
var name:String?=null
}
向适配器添加数据时遇到问题,
如下更新您的适配器
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()}
还有一点小事,请在使用!!
时考虑,在使用
示例,而不是
TestList!!.add(Test)
尝试
TestList?.let {
it.add(Test)
}
正如我在评论中提到的——我对 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
。