我不太清楚 Intent 对象以及如何使用它在活动之间传递数据。在我的应用程序中,我有几个选项卡,我想在它们之间传递 ArrayList。这是我计划使用的示例代码,但我错过了主要活动捕获 Intent 并将其传递给 start 的新活动的部分:
1. myTabs.java ==> 这是我认为我需要添加一些代码来在 TabOne 和 TabTwo 之间传递数据的地方。现在它只是使用 TabActivity 示例的示例代码。
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过程中添加了一段代码,用我要传递给TabTwo的数组填充Intent数据。不确定这是不是正确的方法。
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 ==> 在这里,我试图从 Activity 启动时应该传递的 Intent 中获取 ArrayList。但是如何做到这一点?
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");
}
}
谢谢你的想法!
编辑 :
好的,我会简单点,这是项目的完整工作区:
http://www.lecompteestbon.com/Android.LCEB.zip
我想做的是 lecompteestbon 网站的离线版本,它允许人们在周末之后在朋友之间做会计。
TabPeople = Add the list of friends
TabTransactions = List of expenses
TabTransaction = Add an expense
TabResult = Calculate the list of payments
让我知道如何使这项工作:)