伙计们,我在android studio中遇到了这个问题,我只是使用Recycleview组件,并且一切都做得很好,但是在模拟器中什么也没显示,任何人都可以帮助查找我的代码。.所有项目都是从数据库中提取的。
用于recycleview的项目适配器(itemadapter.kt)
class ItemAdapter (var context: Context,var list:ArrayList):
RecyclerView.Adapter(){ override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): RecyclerView.ViewHolder { var
v:View=LayoutInflater.from(context).inflate(R.layout.item_row,parent) return
ItemHolder(v) }
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position:
Int) {
(holder as ItemHolder).bind(list[position].name,list[position].price,list[position].photo)
}
class ItemHolder(itemView:View):RecyclerView.ViewHolder(itemView)
{
fun bind(n:String,p:String,u:String)
{
itemView.item_name.text =n
itemView.item_price.text=p
var web:String=" http://192.168.43.14/delivery/images/"+ u
web=web.replace("","%20")
Picasso.with(itemView.context).load(web).into(itemView.item_photo)
}
}
}
activity_item.kt
var cat:String=intent.getStringExtra("cat")
var url="http://192.168.43.14/delivery/get_items.php?category= "+cat
var list=ArrayList<Item>()
var rq: RequestQueue = Volley.newRequestQueue(this)
var jar= JsonArrayRequest(Request.Method.GET,url,null, Response.Listener { response ->
for (x in 0..response.length() -1)
list.add(Item(response.getJSONObject(x).getInt("id"),response.getJSONObject(x).getString("name"),
response.getJSONObject(x).getString("price"),response.getJSONObject(x).getString("photo")))
var adp=ItemAdapter(this,list)
item_rv.layoutManager = LinearLayoutManager(this)
item_rv.adapter=adp
}, Response.ErrorListener { error ->
Toast.makeText(this,error.message, Toast.LENGTH_LONG).show()
})
rq.add(jar)
}
}
Item.kt(类文件)
class Item{
var id:Int
var name:String
var price:String
var photo:String
constructor(id:Int,name:String,price:String,photo:String)
{
this.id=id
this.name=name
this.price=price
this.photo=photo
}
}
请大家帮我解决我的问题,因为卡在那儿了〜!
答案 0 :(得分:0)
您忘记了通知列表,请添加LOC adp.notifyDataSetChanged()
var adp=ItemAdapter(this,list)
item_rv.layoutManager = LinearLayoutManager(this)
item_rv.adapter=adp
adp.notifyDataSetChanged()
答案 1 :(得分:0)
首先,请阅读一些教程来学习如何制作一个RecycleView适配器和支架:
您的适配器应如下所示:
class ItemAdapter (val context: Context, val list:ArrayList<Item>): RecyclerView.Adapter<ItemHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
val v: View = LayoutInflater.from(context).inflate(R.layout.item_row, parent, false)
//.inflate(R.layout.item_row, parent, false) *false is important
return ItemHolder(v)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: ItemHolder, position: Int) {
holder.itemName.text = "${list[position].name}"
holder.itemPrice.text = "${list[position].price}"
val web:String="http://192.168.43.14/delivery/images/${list[position].photo}".replace(" ","%20")
Picasso.with(holder.itemView.context).load(web).into(holder.item_photo)
}
}
class ItemHolder(itemView:View):RecyclerView.ViewHolder(itemView) {
val itemName = itemView.item_name
val itemPrice = itemView.item_price
val itemPhoto = itemView.item_photo
}
您可以给我买咖啡:)下一个问题
仅供参考
var item = "item VARIABLE"
val item = "item VALUE"