Passing ArrayList<String> between tabs

丶灬走出姿态 提交于 2019-11-30 19:12:35

问题


I'm not very clear about the Intent object and how to use it to pass data between Activities. In my application I have several tabs between which I want to pass ArrayList. Here is a sample code I plan to use, but I'm missing the part where the main activity catches the Intent and passes it to the new activity on start :

1. myTabs.java ==> This is where I think I need to add some code to pass the data between TabOne and TabTwo. For now it is just using the sample code of the TabActivity sample.

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 ==> I added a piece of code in the onStop procedure to fill in the Intent data with the array I want to pass to TabTwo. Not sure it is the right way to do though.

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 ==> Here I am trying to fetch the ArrayList from the Intent that is supposed to be passed when the Activity starts. But how to do this?

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");
    }
}

Thanks for your ideas !

EDIT :

Okay i'll make it simple, here is the complete workspace of the project :

http://www.lecompteestbon.com/Android.LCEB.zip

What i want to do is an offline version of the website lecompteestbon, which allows people to do accounting between friends after a weekend.

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

Let me know how to make this work :)


回答1:


What's about setting a static field into the myTabs object?

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

On TabOne.java:

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

On TabTwo.java:

arrayPeople =  myTabs.arrayPeople

Does make sense?




回答2:


With the examples above, it was really work when I replace the "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);
}



回答3:


I don't think altering the intent in onStop() will work because the TabHost handles the intents. Setting one class' intent with this.setIntent() wont let another class access it with getIntent() as they are built on different intents.

I would either store my arrayPeople in a database or I would pass arrayPeople back to the TabHost with MyTabs.setArrayPeople or something similar. Then you can query the db onCreate of your next tab or just pull it from your MyTabs Class.



来源:https://stackoverflow.com/questions/2979778/passing-arrayliststring-between-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!