skip to Main Content

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

enter image description here

2

Answers


  1. also, you have to create more model class like below

    Facilities.kt

    data class Facilities(
    val data: List<FacilitiesData>)
    

    FacilitiesData.kt

    data class FacilitiesData(
    val attributes: AttributesData,
    val id: Int)
    

    AttributesData.kt

    data class AttributesData(
    val createdAt: String,
    val name: String,
    val publishedAt: String,
    val updatedAt: String)
    

    in your adapter in onBindViewHolder get your facilities list like below:

        val strFacilites = ArrayList<String>()
    
        for (i in facilities[position].data.indices){
           strFacilites.add(facilities[position].data[i].attributes.name)
        }
    

    That’s it, do Whatever you want with the strFacilites list.

    Login or Signup to reply.
  2. Create a Model class DataModel.kt

    public class DataModel implements Serializable {
        @SerializedName("data")
        private Data data;
    
        public Data getData() {
            return data;
        }
    
        public void setData(Data data) {
            this.data = data;
        }
    
        public static class Data {
    
            @SerializedName("id")
            private Integer id;
            @SerializedName("attributes")
            private Attributes attributes;
    
            public Integer getId() {
                return id;
            }
    
            public void setId(Integer id) {
                this.id = id;
            }
    
            public Attributes getAttributes() {
                return attributes;
            }
    
            public void setAttributes(Attributes attributes) {
                this.attributes = attributes;
            }
    
            public static class Attributes {
    
                @SerializedName("name")
                private String name;
                @SerializedName("tag")
                private String tag;
                @SerializedName("price")
                private String price;
                @SerializedName("rate")
                private Integer rate;
                @SerializedName("description")
                private String description;
                @SerializedName("time")
                private String time;
                @SerializedName("days")
                private String days;
                @SerializedName("createdAt")
                private String createdAt;
                @SerializedName("updatedAt")
                private String updatedAt;
                @SerializedName("publishedAt")
                private Object publishedAt;
                @SerializedName("place")
                private String place;
                @SerializedName("recommended")
                private Boolean recommended;
                @SerializedName("facilities")
                private Facilities facilities;
    
                public String getName() {
                    return name;
                }
    
                public void setName(String name) {
                    this.name = name;
                }
    
                public String getTag() {
                    return tag;
                }
    
                public void setTag(String tag) {
                    this.tag = tag;
                }
    
                public String getPrice() {
                    return price;
                }
    
                public void setPrice(String price) {
                    this.price = price;
                }
    
                public Integer getRate() {
                    return rate;
                }
    
                public void setRate(Integer rate) {
                    this.rate = rate;
                }
    
                public String getDescription() {
                    return description;
                }
    
                public void setDescription(String description) {
                    this.description = description;
                }
    
                public String getTime() {
                    return time;
                }
    
                public void setTime(String time) {
                    this.time = time;
                }
    
                public String getDays() {
                    return days;
                }
    
                public void setDays(String days) {
                    this.days = days;
                }
    
                public String getCreatedAt() {
                    return createdAt;
                }
    
                public void setCreatedAt(String createdAt) {
                    this.createdAt = createdAt;
                }
    
                public String getUpdatedAt() {
                    return updatedAt;
                }
    
                public void setUpdatedAt(String updatedAt) {
                    this.updatedAt = updatedAt;
                }
    
                public Object getPublishedAt() {
                    return publishedAt;
                }
    
                public void setPublishedAt(Object publishedAt) {
                    this.publishedAt = publishedAt;
                }
    
                public String getPlace() {
                    return place;
                }
    
                public void setPlace(String place) {
                    this.place = place;
                }
    
                public Boolean getRecommended() {
                    return recommended;
                }
    
                public void setRecommended(Boolean recommended) {
                    this.recommended = recommended;
                }
    
                public Facilities getFacilities() {
                    return facilities;
                }
    
                public void setFacilities(Facilities facilities) {
                    this.facilities = facilities;
                }
    
                public static class Facilities {
    
                    @SerializedName("data")
                    private List<Datum> data;
    
                    public List<Datum> getData() {
                        return data;
                    }
    
                    public void setData(List<Datum> data) {
                        this.data = data;
                    }
    
                    public static class Datum {
    
                        @SerializedName("id")
                        private Integer id;
                        @SerializedName("attributes")
                        private Attributes__1 attributes;
    
                        public Integer getId() {
                            return id;
                        }
    
                        public void setId(Integer id) {
                            this.id = id;
                        }
    
                        public Attributes__1 getAttributes() {
                            return attributes;
                        }
    
                        public void setAttributes(Attributes__1 attributes) {
                            this.attributes = attributes;
                        }
    
                        public static class Attributes__1 {
    
                            @SerializedName("createdAt")
                            private String createdAt;
                            @SerializedName("updatedAt")
                            private String updatedAt;
                            @SerializedName("publishedAt")
                            private String publishedAt;
                            @SerializedName("name")
                            private String name;
    
                            public String getCreatedAt() {
                                return createdAt;
                            }
    
                            public void setCreatedAt(String createdAt) {
                                this.createdAt = createdAt;
                            }
    
                            public String getUpdatedAt() {
                                return updatedAt;
                            }
    
                            public void setUpdatedAt(String updatedAt) {
                                this.updatedAt = updatedAt;
                            }
    
                            public String getPublishedAt() {
                                return publishedAt;
                            }
    
                            public void setPublishedAt(String publishedAt) {
                                this.publishedAt = publishedAt;
                            }
    
                            public String getName() {
                                return name;
                            }
    
                            public void setName(String name) {
                                this.name = name;
                            }
    
                        }
    
                    }
    
                }
    
            }
    
        }
    }
    

    In Adapter you can pass dataModel object and send it in intent like this :

    holder.cardview.setOnClickListener {
                val intent = Intent(it.context, DetailActivity::class.java)
                intent.putExtra("data_model", dataModel)
                it.context.startActivity(intent)
            }
    

    In DetailActivity :

    Intent intent = getIntent();
    DataModel dataModel = (DataModel) intent.getSerializableExtra("data_model");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search