问题描述
我不是很清楚的意图对象以及如何使用它来传递活动之间的数据。在我的应用程序有几个选项卡之间,我想传递的ArrayList。下面是一个示例code我打算使用,但我很想念那里的主要活动捕获的意图,并将其传递到启动新的活动的一部分:
I'm not very clear about the Intent object and how to use it to pass data between Activities. In my application I have several tabs between which I want to pass ArrayList. Here is a sample code I plan to use, but I'm missing the part where the main activity catches the Intent and passes it to the new activity on start :
1 myTabs.java ==>在这里,我想我需要添加一些code通过塔博恩和TabTwo之间的数据。现在它只是使用TabActivity样本的抽样code。
1. myTabs.java ==> This is where I think I need to add some code to pass the data between TabOne and TabTwo. For now it is just using the sample code of the TabActivity sample.
public class myTabs extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, TabPeopleActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("TabOne").setIndicator("TabOne",
res.getDrawable(R.drawable.ic_tab_one))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, TabTransactionActivity.class);
spec = tabHost.newTabSpec("TabTwo").setIndicator("TabTwo",
res.getDrawable(R.drawable.ic_tab_two))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
2 TabOne.java ==>我的的onStop过程中添加了一块$ C $三来一补与我想传递给TabTwo阵列的意图数据。不知道这是正确的方式做,但。
2. TabOne.java ==> I added a piece of code in the onStop procedure to fill in the Intent data with the array I want to pass to TabTwo. Not sure it is the right way to do though.
public class TabOne extends Activity {
[...]
private ArrayList<String> arrayPeople;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabone);
arrayPeople = new ArrayList<String>();
[... here we modify arrayPeople ...]
}
/** Called when the activity looses focus **/
@Override
public void onStop(){
Intent myIntent = new Intent();
myIntent.putStringArrayListExtra("arrayPeople", arrayPeople);
this.setIntent(myIntent);
}
}
3。 TabTwo.java ==>在这里,我想从提取是应该在活动启动时要传递的意图ArrayList中。但如何做到这一点?
3. TabTwo.java ==> Here I am trying to fetch the ArrayList from the Intent that is supposed to be passed when the Activity starts. But how to do this?
public class TabTwo extends Activity {
private ArrayList<String> arrayPeople;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transaction);
Intent myIntent = new Intent();
myIntent = this.getIntent();
arrayPeople = myIntent.getStringArrayListExtra("arrayPeople");
}
}
感谢您的想法!
Thanks for your ideas !
编辑:
好吧,我会让它简单,这里是项目的完整工作区:
Okay i'll make it simple, here is the complete workspace of the project :
http://www.lecompteestbon.com/Android.LCEB.zip
我想要做的是网站lecompteestbon,它允许人们做后一个周末朋友之间占的离线版本。
What i want to do is an offline version of the website lecompteestbon, which allows people to do accounting between friends after a weekend.
TabPeople = Add the list of friends
TabTransactions = List of expenses
TabTransaction = Add an expense
TabResult = Calculate the list of payments
让我知道如何使这项工作:)
Let me know how to make this work :)
推荐答案
通过上面的例子,它是真正的工作,当我取代的onStop=的onPause
With the examples above, it was really work when I replace the "onStop" = "onPause"
/** Called when the activity looses focus **/
@Override public void onStop()
{
Intent myIntent = new Intent();
myIntent.putStringArrayListExtra("arrayPeople", arrayPeople);
this.setIntent(myIntent);
}
/** Called when the activity looses focus **/
@Override public void onPause()
{
Intent myIntent = new Intent();
myIntent.putStringArrayListExtra("arrayPeople", arrayPeople);
this.setIntent(myIntent);
}
这篇关于路过的ArrayList&LT;字符串&GT;标签之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!