我想使用Intent从"facilities"中获取字段"name",然后在textview/Recyclerview中进行替换。我该怎么做?
下面是我的适配器、响应API和活动。
这是一个API响应:
{
"data": {
"id": 7,
"attributes": {
"name": "Ancol",
"tag": "Wisata Jakarta",
"price": "100000",
"rate": 3,
"description": "deskripsi disini",
"time": "10.00 WIB",
"days": "Selasa-Minggu",
"createdAt": "2023-02-23T09:26:10.385Z",
"updatedAt": "2023-02-23T09:26:10.385Z",
"publishedAt": null,
"place": "Jakarta",
"recommended": true,
"facilities": {
"data": [
{
"id": 1,
"attributes": {
"createdAt": "2023-02-18T10:17:25.594Z",
"updatedAt": "2023-02-23T09:03:29.577Z",
"publishedAt": "2023-02-18T10:17:26.529Z",
"name": "Toilet"
}
},
{
"id": 6,
"attributes": {
"createdAt": "2023-02-18T11:23:24.451Z",
"updatedAt": "2023-02-23T09:04:52.636Z",
"publishedAt": "2023-02-18T11:23:24.439Z",
"name": "Transportasi"
}
}
]
}
}
}
}
适配器:
class ListAllAdapter(val allList: List<DataItem?>?) :
RecyclerView.Adapter<ListAllAdapter.MyViewHolder>() {
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val cardview = view.findViewById<CardView>(R.id.cardview)
val thumb = view.findViewById<ImageView>(R.id.thumb)
val name = view.findViewById<TextView>(R.id.tv_name)
val place = view.findViewById<TextView>(R.id.tv_place)
val rate = view.findViewById<TextView>(R.id.tv_rate)
val price = view.findViewById<TextView>(R.id.tv_price)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_list, parent, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
if (allList != null) {
val limit = 15
return allList.size.coerceAtMost(limit)
}
return 0
}
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.name.text = allList?.get(position)?.attributes?.name
holder.place.text = allList?.get(position)?.attributes?.place
holder.rate.text = "Rating : " + allList?.get(position)?.attributes?.rate.toString()
// formating number price
val price = allList?.get(position)?.attributes?.price?.toInt()
val localID = Locale("in", "ID")
val numberFormat = NumberFormat.getCurrencyInstance(localID)
holder.price.text = numberFormat.format(price)
val thumbUrl =
"http://10.0.2.2:1337" + allList?.get(position)?.attributes?.thumb?.data?.attributes?.url
Picasso.get().load(thumbUrl).into(holder.thumb)
holder.cardview.setOnClickListener {
val intent = Intent(it.context, DetailActivity::class.java)
intent.putExtra("id", allList?.get(position)?.id)
intent.putExtra("thumb", thumbUrl)
intent.putExtra("name", holder.name.text)
intent.putExtra("tag", allList?.get(position)?.attributes?.tag)
intent.putExtra("place", holder.place.text)
intent.putExtra("rate", holder.rate.text)
intent.putExtra("price", price)
intent.putExtra("description", allList?.get(position)?.attributes?.description)
intent.putExtra("time", allList?.get(position)?.attributes?.time)
intent.putExtra("days", allList?.get(position)?.attributes?.days)
intent.putExtra("facility", allList?.get(position)?.attributes?.facilities?.data?.get(position)?.attributes?.name)
it.context.startActivity(intent)
}
}
}
活动:
class DetailActivity : AppCompatActivity() {
lateinit var img_thumb : ImageView
lateinit var tv_name : TextView
lateinit var tv_tag : TextView
lateinit var tv_price : TextView
lateinit var tv_description : TextView
lateinit var tv_timedays : TextView
lateinit var tv_facilites: TextView
var name : String = ""
var place : String = ""
var tag : String = ""
var price : Int = 0
var description : String = ""
var time : String = ""
var days : String = ""
// variabel facility
var facilities : String = ""
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
tv_name = findViewById(R.id.tv_name)
tv_tag = findViewById(R.id.tv_tag)
tv_price = findViewById(R.id.tv_price)
tv_description = findViewById(R.id.tv_description)
tv_timedays = findViewById(R.id.tv_timedays)
img_thumb = findViewById(R.id.img_thumb)
name = intent.getStringExtra("name").toString()
place = intent.getStringExtra("place").toString()
tag = intent.getStringExtra("tag").toString()
price = intent.getIntExtra("price",0)
description = intent.getStringExtra("description").toString()
time = intent.getStringExtra("time").toString()
days = intent.getStringExtra("days").toString()
// ambil data facility
facilities = intent.getStringExtra("facility").toString()
tv_name.text = "$name-($place)"
tv_tag.text = tag
val localID = Locale("in","ID")
val numberFormat = NumberFormat.getCurrencyInstance(localID)
tv_price.text = numberFormat.format(price)
tv_description.text = description
tv_timedays.text = "Time : $time || Days : $days"
// set Text view untuk facility
tv_facilites.text = facilities
}
}
那么有没有其他的方法来做这个呢?我以前从来没有做过,请帮帮忙,谢谢
enter image description here
3条答案
按热度按时间gzszwxb41#
您可以使用Serializable或Parcelable通过Intent传递模型/pojo。
参考链接:Link1Link2Link3
xmq68pz92#
此外,您必须创建更多的模型类,如下所示
Facilities.kt
FacilitiesData.kt
AttributesData.kt
在onBindViewHolder中的适配器中,获取如下所示的工具列表:
就是这样,你可以随意处理strFacilites列表。
aiqt4smr3#
创建一个模型类DataModel.kt
在Adapter中,您可以传递dataModel对象并在Intent中发送它,如下所示:
详细活动: