在选项卡之间传递ArrayList <string> </string>

时间:2010-06-05 09:12:01

标签: android android-2.2-froyo

我不太清楚Intent对象以及如何使用它在Activities之间传递数据。在我的应用程序中,我有几个选项卡,我想在其中传递ArrayList。这是我打算使用的示例代码,但是我错过了主要活动捕获Intent并将其传递给start上的新活动的部分:

1。 myTabs.java ==&gt;这是我认为我需要添加一些代码来在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 ==&gt;我在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 ==&gt;在这里,我试图从活动开始时应该传递的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

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

3 个答案:

答案 0 :(得分: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

有意义吗?

答案 1 :(得分: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);
}

答案 2 :(得分:0)

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

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

相关问题