2

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

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

4

3 回答 3

2

通过上面的例子,当我替换“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);
}
于 2011-04-17T15:35:34.180 回答
2

如何在 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

有意义吗?

于 2010-06-05T11:50:54.647 回答
0

我不认为改变 onStop() 中的意图会起作用,因为 TabHost 会处理意图。使用 this.setIntent() 设置一个类的意图不会让另一个类使用 getIntent() 访问它,因为它们是基于不同的意图构建的。

我要么将我的 arrayPeople 存储在数据库中,要么使用 MyTabs.setArrayPeople 或类似的东西将 arrayPeople 传递回 TabHost。然后,您可以查询下一个选项卡的 db onCreate 或从 MyTabs 类中提取它。

于 2010-06-05T10:21:09.553 回答