2017-03-08 25 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

相關問題