Question

I am trying to pass some data to another activity, but I need pass the data when I click in a button , but not start the new activity and when i have multiple past data, launch the new activity and see all the content that I have passed before.

The process is similar to a shopping cart, add products and then another activity you see the list of the cart.

I have been trying with SharePrerences, but I only pass one data.

final SharedPreferences mSettings = this.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = mSettings.edit();    
editor.putString(NOMBRE_TAG, tvNotas.getText().toString());
editor.putString(PRECIO_TAG, pantalla.getText()+"");
editor.commit();

with putExtra I do not think it works, because I dont know how much data I will pass

Intent intent = new Intent(this, NAME.class);
intent.putextra("SOMETHING","value");
startActivity(intent);
String data = getIntent().getExtras().getString("SOMETHING");

And i dont know other forms to do it.

The process is similar like

Was it helpful?

Solution

when you use shared preference every "key" like your tags(NOMBRE_TAG,PRECIO_TAG) will save only one item. which mean, everytime you save item with this keys it will replace the old items.

i recommender using SQLITE database.

here some example how to start:

Android SQLite Example

OTHER TIPS

You could use a static class, or singleton? This way you can put whatever you want in it from the first activity and retrieve it from the second activity. The only drawback is that it will not survive an application restart.

Use putParcelableArrayList to pass all the data as a array list ---

    @Override
    public void onSaveInstanceState(final Bundle outState) {
    outState.putParcelableArrayList("data", myData);
    super.onSaveInstanceState(outState);
    }

Then to get this data just use ----

ArrayList<String> myData = new ArrayList<String>();

@Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
   {
        super.onCreateView(inflater, container, savedInstanceState);

        if (savedInstanceState != null) {

            try
            {
               myData = savedInstanceState.getParcelableArrayList("data");
            }

        }

        myData.add(0, tvNotas.getText().toString());
        myData.add(1, pantalla.getText()+"");
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top