2017-03-08 95 views
-4

下面的代码将json返回到textView。如何将此文本视图中的数据传递给其他活动?将JSON传递给新的活动 - Android应用程序

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。我知道这可以使用intent来完成,但我不确定如何在这个例子中使用它。

感谢

+1

什么问题? –

+0

如何将我在TextView中的数据传递给其他活动? – Mkuuu7

+0

使用intents和putExtra – Orphamiel

回答

0

谢谢大家的回答。这使我更懂得如何使用意图和我能解决我的问题是:

活动1

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

活动2

Bundle b = getIntent().getExtras(); 
    String json = b.getString("Data"); 
    TextView jData = (TextView) findViewById(R.id.textView1); 
    jData.setText(json); 
0
String json=textViewResult.getText().toString(); 
startActivity(new Intent(this,YourActivity.class).putExtra("data",json)); 

这是你想要的吗?

+0

谢谢你的回答。我能够以类似的方式解决我的问题。 – Mkuuu7

0
使用putExtra

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

在SecondActivity从活动

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

OR

通行证JSON数据

传递字符串数据

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

在SecondActivity

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

谢谢你的回答。我能够以类似的方式解决我的问题。 – Mkuuu7

相关问题