如何将JSON值发送到另一个活动Android

时间:2017-03-09 18:00:52

标签: java android json android-intent

所以,我有一个从JSON获取其值的列表:

ListView

JSONArray array = new JSONArray(jsonStr);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    JSONArray paises = jsonObject.optJSONArray("paises");

                    if (paises != null) {
                        for (int j = 0; j < paises.length(); j++) {
                            JSONObject jsonObject1 = paises.getJSONObject(j);
                            System.out.println(jsonObject1.optString("Designacao"));
                            String K_PAIS = jsonObject1.getString("K_PAIS");
                            String Designacao = jsonObject1.getString("Designacao");
                            String URL_IMAGE_SMALL = jsonObject1.getString("URL_IMAGE_SMALL");
                            String Coord_LAT = jsonObject1.getString("Coord_LAT");
                            String Coord_LONG = jsonObject1.getString("Coord_LONG");
                            String Coord_Zoom = jsonObject1.getString("Coord_Zoom");

                            HashMap<String, String> pais = new HashMap<>();

                            pais.put("K_PAIS", K_PAIS);
                            pais.put("Designacao", Designacao);
                            pais.put("URL_IMAGE_SMALL", URL_IMAGE_SMALL);
                            pais.put("URL_IMAGEM", URL_IMAGEM);
                            pais.put("Coord_LAT",Coord_LAT);
                            pais.put("Coord_LONG",Coord_LONG);
                            pais.put("Coord_Zoom",Coord_Zoom);
                            listaPaises.add(pais);
                        }
                    }
                }

现在我想将这些价值发送给我的下一个活动。 这是我的点击监听器:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = new Intent(view.getContext(), MapsActivity.class);
                intent.putExtra(  ,  );
                startActivity(intent);

            }
        });

我知道它类似intent.putExtra( , );,但我不知道要发送什么变量,因为值与项目不同。 我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

创建一个实现Serializable的对象:

import java.io.Serializable;

public class Example implements Serializable {

    private String mName;
    private String mId;
    private int mNumber;

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

    public String getId() {
        return mId;
    }

    public void setId(String id) {
        mId = id;
    }

    public int getNumber() {
        return mNumber;
    }

    public void setNumber(int number) {
        mNumber = number;
    }
}

然后你可以把这个额外的东西拿到getSerializableExtra()

intent.putExtra(EXTRA_EXAMPLE, example);
Example example = (Example) intent.getSerializableExtra(EXTRA_EXAMPLE);

答案 1 :(得分:0)

ArrayList<String> listpais= new ArrayList<String>(); // make this global   
JSONArray array = new JSONArray(jsonStr);
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject jsonObject = array.getJSONObject(i);
                        JSONArray paises = jsonObject.optJSONArray("paises");

                        if (paises != null) {
                            for (int j = 0; j < paises.length(); j++) {
                                JSONObject jsonObject1 = paises.getJSONObject(j);
                                System.out.println(jsonObject1.optString("Designacao"));
                                String K_PAIS = jsonObject1.getString("K_PAIS");
                                String Designacao = jsonObject1.getString("Designacao");
                                String URL_IMAGE_SMALL = jsonObject1.getString("URL_IMAGE_SMALL");
                                String Coord_LAT = jsonObject1.getString("Coord_LAT");
                                String Coord_LONG = jsonObject1.getString("Coord_LONG");
                                String Coord_Zoom = jsonObject1.getString("Coord_Zoom");

                                someClass xyz= new someClass();

                                xyz.var1=K_data1;
                                xyz.var2=K_data2;
                                xyz.var3=K_data3;
                                xyz.var4=K_data4;
                                xyz.var5=K_data5;

                                listaPaises.add(xyz);
                            }
                        }
                    }

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent intent = new Intent(view.getContext(), MapsActivity.class);
                intent.putExtra(  "data1", listpais );
                startActivity(intent);

            }
        });

答案 2 :(得分:0)

制作一个方法来获取适配器内的点击项目,如下所示

public Object getItemByPosition(int position){
//Array is your array of objects defined inside the adapter
return array.get(position).toString();
}

然后在你的项目内点击listerner你可以做

Intent intent = new Intent(view.getContext(), MapsActivity.class);
            intent.putExtra("your_key",  adapter.getItemByPosition(position));
            startActivity(intent);