如何将关联数组从一个片段发送到另一个片段? [重复]

时间:2022-10-30 12:48:35

This question already has an answer here:

这个问题在这里已有答案:

I have a "MainActivity" containing a fragment "fragment1", this fragment shows a loader until data is fetched using Volley library. After this fragment1 gets the JSONResponse, it has to pass the data to the second fragment fragment2. My data from JSON are as follows:

我有一个包含片段“fragment1”的“MainActivity”,这个片段显示了一个加载器,直到使用Volley库获取数据。在此fragment1获取JSONResponse之后,它必须将数据传递给第二个片段fragment2。我从JSON获得的数据如下:

{ "city": ["Moscow", "Istanbul"],
 "Moscow":["Moscow Kremlin", "Red Square", "Gorky Park", "Saint Pittsburgh"],
"Istanbul":["Taksim Square", "Hagia Sophia", "Grand Bazaar", "Spice Bazaar"]}

The problem here is that the number of cities and places are not fixed. Information keeps adding up. I've completed the data retrieval process on fragment1 but I'm unsure how to send it to the second fragment are retrieve the information there. Do I use Bundle or the onFragmentInteractionListener? And since there are no associative arrays in Java, how to I accomplish this?

这里的问题是城市和地方的数量不固定。信息不断增加。我已经完成了fragment1上的数据检索过程,但我不确定如何将它发送到第二个片段,检索那里的信息。我是否使用Bundle或onFragmentInteractionListener?既然Java中没有关联数组,我该如何实现呢?

1 个解决方案

#1


1  

From below i will show you how to save and get values,

从下面我会告诉你如何保存和获取价值,

           Gson gson = new Gson();
           Example profile = new Example();
           Type listType = new TypeToken<Example>() {
                }.getType(); 
           profile = gson.fromJson(yourjson, listType);
           List<String> city = profile.getCity();

Example Class :

示例类:

        package com.example;

    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Example {

    @SerializedName("city")
    @Expose
    private List<String> city = null;
    @SerializedName("Moscow")
    @Expose
    private List<String> moscow = null;
    @SerializedName("Istanbul")
    @Expose
    private List<String> istanbul = null;

    public List<String> getCity() {
    return city;
    }

    public void setCity(List<String> city) {
    this.city = city;
    }

    public List<String> getMoscow() {
    return moscow;
    }

    public void setMoscow(List<String> moscow) {
    this.moscow = moscow;
    }

    public List<String> getIstanbul() {
    return istanbul;
    }

    public void setIstanbul(List<String> istanbul) {
    this.istanbul = istanbul;
    }

}

#1


1  

From below i will show you how to save and get values,

从下面我会告诉你如何保存和获取价值,

           Gson gson = new Gson();
           Example profile = new Example();
           Type listType = new TypeToken<Example>() {
                }.getType(); 
           profile = gson.fromJson(yourjson, listType);
           List<String> city = profile.getCity();

Example Class :

示例类:

        package com.example;

    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class Example {

    @SerializedName("city")
    @Expose
    private List<String> city = null;
    @SerializedName("Moscow")
    @Expose
    private List<String> moscow = null;
    @SerializedName("Istanbul")
    @Expose
    private List<String> istanbul = null;

    public List<String> getCity() {
    return city;
    }

    public void setCity(List<String> city) {
    this.city = city;
    }

    public List<String> getMoscow() {
    return moscow;
    }

    public void setMoscow(List<String> moscow) {
    this.moscow = moscow;
    }

    public List<String> getIstanbul() {
    return istanbul;
    }

    public void setIstanbul(List<String> istanbul) {
    this.istanbul = istanbul;
    }

}