我有两个活动。在按钮上点击主要活动对应的Asynctask被调用(2个asynctask在那里)并且需要在另一个活动的文本视图中显示那些已解析的Json数据(几乎10个文本视图)。如何做吗?。
答案 0 :(得分:1)
在您的AsyncTask的onPostExecute()中,您可以创建一个Intent并将数据作为额外内容放入您的意图中并使用该意图重定向到您的第二个活动,在第二个活动的onCreate()内读取您的意图的额外内容。代码将类似......
onPostExecute(...){ //pass your data here
Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
myIntent.putExtra("MY_FIRST_TEXTVIEW_DATA","Value_of_first_tv_data");
....//Here you can put as many extras as you wish, your can also put an object with multiple values.
....
startActivity(myIntent); //this will take to SecondAcitivity.
}
一旦控件进入SecondActivity,就在onCreate()
中....onCreate(...){
....
setContentView(...);
String firstTextViewData = getIntent().getStringExtra("MY_FIRST_TEXTVIEW_DATA");
//the value that you passed in myIntent with tag MY_FIRST_TEXTVIEW_DATA will be received inside firstTextViewData, now you can set it on a textview.
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(firstTextViewData);
}
希望你对如何做到这一点有所了解。让我知道。感谢