【问题标题】:Passing JSON to new activity - Android app [duplicate]将 JSON 传递给新活动 - Android 应用程序 [重复]
【发布时间】:2017-03-08 02:30:20
【问题描述】:

以下代码将 json 返回到 textView。如何将此文本视图中的数据传递给另一个活动?

 private void showJSON(String response){
    String name="";
    String address="";
    String vc = "";
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject collegeData = result.getJSONObject(0);
        name = collegeData.getString(Config.KEY_NAME);
        address = collegeData.getString(Config.KEY_ADDRESS);
        vc = collegeData.getString(Config.KEY_VC);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    textViewResult.setText("Name:\t"+name+"\nAddress:\t" +address+ "\nVice Chancellor:\t"+ vc);
}

这是我关注的tutorial link。我知道它可以使用意图来完成,但我不确定如何在这个例子中使用它。

谢谢

【问题讨论】:

  • 问题是什么?
  • 如何将 TextView 中的数据传递给另一个活动?
  • 使用intents和putExtra
  • 如果你告诉我它在这个例子中是如何工作的可以吗?

标签: java android json


【解决方案1】:
String json=textViewResult.getText().toString();
startActivity(new Intent(this,YourActivity.class).putExtra("data",json));

这是你想要的吗?

【讨论】:

  • 感谢您的回答。我能够以类似的方式解决我的问题。
【解决方案2】:

使用 putExtra 传递字符串数据

    Intent i = new Intent(youractivity.this, SecondActivity.class);
    i.putExtra("name",name);
    i.putExtra("address",address);
    i.putExtra("vc",vc);
    startActivity(i);

在第二活动中

    String name=getIntent().getStringExtra("name"); 
    String address=getIntent().getStringExtra("address"); 
    String vc=getIntent().getStringExtra("vc"); 

从活动中传递 json 数据

     Intent i = new Intent(youractivity.this, SecondActivity.class);
     intent.putExtra("jsonObject", jsonObject.toString());
     startActivity(i);

在第二活动中

      Intent intent = getIntent();
      String jsonString = intent.getStringExtra("jsonObject");
      JSONObject jsonObject = new JSONObject(jsonString);

【讨论】:

  • 感谢您的回答。我能够以类似的方式解决我的问题。
【解决方案3】:

谢谢大家的回答。这帮助我了解了如何使用意图,并​​且我能够通过以下方式解决我的问题:

活动一

    String json= textViewResult.getText().toString();
    Intent i = new Intent(Activity1.this, Activity2.class);
    i.putExtra("Data",json);
    startActivity(i);

活动二

    Bundle b = getIntent().getExtras();
    String json = b.getString("Data");
    TextView jData = (TextView) findViewById(R.id.textView1);
    jData.setText(json);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多