回收站视图未在 kotlin 中显示 JSON 数据
Recycler View not showing JSON data in kotlin
这是我的主要 class,我在其中使用 volley 在 ArrayList 中添加 JASON 数据。
Toast 显示 JASON 数据,但数组不显示任何数据。我正在尝试解决过去 3 天的错误。
我也在堆栈上阅读了很多问题,但我没有解决这个问题请帮助我
var item = ArrayList<dumy_item_list>()
var url = "https://apps.faizeqamar.website/charity/api/organizations"
var rq: RequestQueue = Volley.newRequestQueue(this)
var sr = StringRequest(Request.Method.GET, url, Response.Listener { response ->
var jsonResponse = JSONObject(response)
var jsonArray: JSONArray = jsonResponse.getJSONArray("data")
for (i in 0..jsonArray.length() - 1) {
var jsonObject: JSONObject = jsonArray.getJSONObject(i)
var name = jsonObject.getString("name")
val data = dumy_item_list()
data.setName(jsonObject.getString(name))
item.add(data)
Toast.makeText(applicationContext, "NGO Name is : $name", Toast.LENGTH_LONG).show()
}
},
Response.ErrorListener { error ->
Toast.makeText(applicationContext, error.message, Toast.LENGTH_LONG).show()
})
rq.add(sr)
var away_recycler = findViewById<RecyclerView>(R.id.away_recycler)
var adaptor = custom_adopter(item, applicationContext)
away_recycler.layoutManager = GridLayoutManager(applicationContext, 1)
away_recycler.adapter = adaptor
}
这是我的适配器 class,我在其中使用 getName() 函数
class custom_adopter(data: ArrayList<dumy_item_list>, var context: Context) :
RecyclerView.Adapter<custom_adopter.viewHolder>() {
var data: List<dumy_item_list>
init {
this.data = data
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): custom_adopter.viewHolder {
var layout = LayoutInflater.from(context).inflate(R.layout.dumy_item, parent, false)
return viewHolder(layout)
}
override fun onBindViewHolder(holder: custom_adopter.viewHolder, position: Int) {
holder.tv_dummy_name_donnor.text = data[position].getName()
holder.card.setOnClickListener {
var intent = Intent(context, ngosProfile::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(context, intent, null)
}
}
override fun getItemCount(): Int {
return data.size
}
class viewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
internal var tv_dummy_name_donnor: TextView
internal var card: CardView
init {
tv_dummy_name_donnor = itemView.findViewById(R.id.tv_dummy_name_donnor)
card = itemView.findViewById(R.id.card)
}
}
}
我猜你有包含所有项目的 RecyclerView,但它们是空的
所以问题应该出在你填写适配器列表的地方......在下面这一行中:
data.setName(jsonObject.getString(name))
一定是
data.setName(name)
或
data.setName(jsonObject.getString("name"))
你应该在列表中加载数据后调用适配器的notifyDataSetChanged方法,以通知有新数据。
遵循此代码对我有用。 (var adaptor = custom_adopter(item, applicationContext)
away_recycler.adapter = adaptor
progressBar2?.visibility = View.INVISIBLE )
循环后将值传递给适配器。
class MainActivity : AppCompatActivity() {
var progressBar2:ProgressBar?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var item = ArrayList<dumy_item_list>()
var progressBar2 = findViewById<ProgressBar>(R.id.progressBar2)
var away_recycler = findViewById<RecyclerView>(R.id.away_recycler)
away_recycler.layoutManager = GridLayoutManager(applicationContext, 1)
var url = "https://apps.faizeqamar.website/charity/api/organizations"
var rq: RequestQueue = Volley.newRequestQueue(this)
var sr = StringRequest(Request.Method.GET, url, Response.Listener { response ->
var jsonResponse = JSONObject(response)
var jsonArray: JSONArray = jsonResponse.getJSONArray("data")
for (i in 0..jsonArray.length() - 1) {
var jsonObject: JSONObject = jsonArray.getJSONObject(i)
var name = jsonObject.getString("ngo_name")
var about = jsonObject.getString("ngo_desc")
item.add(dumy_item_list(name,about))
}
var adaptor = custom_adopter(item, applicationContext)
away_recycler.adapter = adaptor
progressBar2?.visibility = View.INVISIBLE
},
Response.ErrorListener { error ->
})
rq.add(sr)
}
这是我的主要 class,我在其中使用 volley 在 ArrayList 中添加 JASON 数据。 Toast 显示 JASON 数据,但数组不显示任何数据。我正在尝试解决过去 3 天的错误。 我也在堆栈上阅读了很多问题,但我没有解决这个问题请帮助我
var item = ArrayList<dumy_item_list>()
var url = "https://apps.faizeqamar.website/charity/api/organizations"
var rq: RequestQueue = Volley.newRequestQueue(this)
var sr = StringRequest(Request.Method.GET, url, Response.Listener { response ->
var jsonResponse = JSONObject(response)
var jsonArray: JSONArray = jsonResponse.getJSONArray("data")
for (i in 0..jsonArray.length() - 1) {
var jsonObject: JSONObject = jsonArray.getJSONObject(i)
var name = jsonObject.getString("name")
val data = dumy_item_list()
data.setName(jsonObject.getString(name))
item.add(data)
Toast.makeText(applicationContext, "NGO Name is : $name", Toast.LENGTH_LONG).show()
}
},
Response.ErrorListener { error ->
Toast.makeText(applicationContext, error.message, Toast.LENGTH_LONG).show()
})
rq.add(sr)
var away_recycler = findViewById<RecyclerView>(R.id.away_recycler)
var adaptor = custom_adopter(item, applicationContext)
away_recycler.layoutManager = GridLayoutManager(applicationContext, 1)
away_recycler.adapter = adaptor
}
这是我的适配器 class,我在其中使用 getName() 函数
class custom_adopter(data: ArrayList<dumy_item_list>, var context: Context) :
RecyclerView.Adapter<custom_adopter.viewHolder>() {
var data: List<dumy_item_list>
init {
this.data = data
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): custom_adopter.viewHolder {
var layout = LayoutInflater.from(context).inflate(R.layout.dumy_item, parent, false)
return viewHolder(layout)
}
override fun onBindViewHolder(holder: custom_adopter.viewHolder, position: Int) {
holder.tv_dummy_name_donnor.text = data[position].getName()
holder.card.setOnClickListener {
var intent = Intent(context, ngosProfile::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(context, intent, null)
}
}
override fun getItemCount(): Int {
return data.size
}
class viewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
internal var tv_dummy_name_donnor: TextView
internal var card: CardView
init {
tv_dummy_name_donnor = itemView.findViewById(R.id.tv_dummy_name_donnor)
card = itemView.findViewById(R.id.card)
}
}
}
我猜你有包含所有项目的 RecyclerView,但它们是空的 所以问题应该出在你填写适配器列表的地方......在下面这一行中:
data.setName(jsonObject.getString(name))
一定是
data.setName(name)
或
data.setName(jsonObject.getString("name"))
你应该在列表中加载数据后调用适配器的notifyDataSetChanged方法,以通知有新数据。
遵循此代码对我有用。 (var adaptor = custom_adopter(item, applicationContext)
away_recycler.adapter = adaptor
progressBar2?.visibility = View.INVISIBLE )
循环后将值传递给适配器。
class MainActivity : AppCompatActivity() {
var progressBar2:ProgressBar?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var item = ArrayList<dumy_item_list>()
var progressBar2 = findViewById<ProgressBar>(R.id.progressBar2)
var away_recycler = findViewById<RecyclerView>(R.id.away_recycler)
away_recycler.layoutManager = GridLayoutManager(applicationContext, 1)
var url = "https://apps.faizeqamar.website/charity/api/organizations"
var rq: RequestQueue = Volley.newRequestQueue(this)
var sr = StringRequest(Request.Method.GET, url, Response.Listener { response ->
var jsonResponse = JSONObject(response)
var jsonArray: JSONArray = jsonResponse.getJSONArray("data")
for (i in 0..jsonArray.length() - 1) {
var jsonObject: JSONObject = jsonArray.getJSONObject(i)
var name = jsonObject.getString("ngo_name")
var about = jsonObject.getString("ngo_desc")
item.add(dumy_item_list(name,about))
}
var adaptor = custom_adopter(item, applicationContext)
away_recycler.adapter = adaptor
progressBar2?.visibility = View.INVISIBLE
},
Response.ErrorListener { error ->
})
rq.add(sr)
}