如何在Kotlin中使用Intent传递对象数组

ejk8hzay  于 2023-02-24  发布在  Kotlin
关注(0)|答案(3)|浏览(174)

我想使用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

gzszwxb4

gzszwxb41#

您可以使用SerializableParcelable通过Intent传递模型/pojo。
参考链接:Link1Link2Link3

xmq68pz9

xmq68pz92#

此外,您必须创建更多的模型类,如下所示
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)

在onBindViewHolder中的适配器中,获取如下所示的工具列表:

val strFacilites = ArrayList<String>()

    for (i in facilities[position].data.indices){
       strFacilites.add(facilities[position].data[i].attributes.name)
    }

就是这样,你可以随意处理strFacilites列表。

aiqt4smr

aiqt4smr3#

创建一个模型类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;
                        }

                    }

                }

            }

        }

    }
}

在Adapter中,您可以传递dataModel对象并在Intent中发送它,如下所示:

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

详细活动:

Intent intent = getIntent();
DataModel dataModel = (DataModel) intent.getSerializableExtra("data_model");

相关问题