I want to get the field "name" from "facilities" using intents and then replace in textview / Recyclerview. How do I do that?
Here are my adapters, response APIs, and activities.
This is an API response:
{
"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"
}
}
]
}
}
}
}
Adapter :
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)
}
}
}
Activity :
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
}
}
So Is there any way to do this in other way?
I’ve never done this before. Please help.
Thanks
2
Answers
also, you have to create more model class like below
Facilities.kt
FacilitiesData.kt
AttributesData.kt
in your adapter in onBindViewHolder get your facilities list like below:
That’s it, do Whatever you want with the strFacilites list.
Create a Model class DataModel.kt
In Adapter you can pass dataModel object and send it in intent like this :
In DetailActivity :