我不是很清楚Intent對象以及如何使用它在活動之間傳遞數據。在我的應用程序中,我有幾個要在其間傳遞ArrayList的選項卡。以下是我計劃使用的示例代碼,但我錯過了主要活動捕捉到Intent並將其傳遞給開始時的新活動的部分:在標籤之間傳遞ArrayList <String>
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陣列的意圖的數據。不知道這是正確的做法。
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);
}
}
TabTwo.java ==>在這裏,我試圖來從一個應該在活動開始時要傳遞的意圖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
讓我知道如何使這項工作:)
使用數據庫似乎太先進了這個簡單的東西它是真正的工作。當你的意思是使用「MyTabs.setArrayPeople」時,它是否與上面提出的類似? – Christophe 2010-06-06 04:33:01
是的,使用靜態ArrayList將是最好的。 – m6tt 2010-06-06 10:46:30