android - 在选项卡之间传递 ArrayList<String>

标签 android android-2.2-froyo

我不太清楚 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/

相关文章:

android - Gradle 任务发布/上传 android apk 到 Http 服务器

android - 可绘制对象与位图

Android 后退按钮覆盖帮助

java - 如何等待线程和处理程序初始化

android - 如何在android中一一使用具有多个值的文本语音

android - 了解安卓: Zygote and DalvikVM

安卓服务启动时间

java - 在制作新 Activity 时专注于文本的特定部分

android - 在 Froyo 上 move 文件

java - Android 2.2 SDK - Droid X 相机 Activity 未正确完成