gpt4 book ai didi

android - 在选项卡之间传递 ArrayList

转载 作者:搜寻专家 更新时间:2023-11-01 08:17:13 25 4
gpt4 key购买 nike

我不是很清楚 Intent 对象以及如何使用它在 Activity 之间传递数据。在我的应用程序中,我有几个选项卡,我想在它们之间传递 ArrayList。这是我打算使用的示例代码,但我遗漏了主要 Activity 捕获 Intent 并将其传递给开始时的新 Activity 的部分:

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 网站的离线版本,它允许人们在周末后在 friend 之间做账。

TabPeople = Add the list of friends
TabTransactions = List of expenses
TabTransaction = Add an expense
TabResult = Calculate the list of payments

让我知道如何完成这项工作:)

最佳答案

如何在 myTabs 对象中设置一个静态字段?

    public class myTabs extends TabActivity {
....
public static arrayPeople = new ArrayList<String>();
....

在 TabOne.java 上:

 @Override
public void onStop(){
myTabs.arrayPeople = arrayPeople;
}

在 TabTwo.java 上:

arrayPeople =  myTabs.arrayPeople

有意义吗?

关于android - 在选项卡之间传递 ArrayList<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2979778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com