Store data temporarily on android

1

I have created a shopping cart in Android, the user has the option to see a product in a ListView, then clicking on it goes to an activity where he sees the product detail and has the option to confirm the purchase, until now all right but I want to give the option to continue buying and until the end confirm the whole purchase, here is where I do not know what is the best option if you save each order temporarily in the BD or if there is another way to temporarily save the order and go adding more products. I had thought about creating an ArrayList but every time I enter the activity detail that Array is restarted.

btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(),codigo,Toast.LENGTH_LONG).show();
            pedido.add(codigo);
        }
    })
    
asked by Igmer Rodriguez 26.10.2018 в 23:25
source

2 answers

2

If it's temporary, you may be able to use the preferences, but a database would be recommended.

I'll give you an example of the preferences.

SharedPreferences prefs = getSharedPreferences("MisPreferencias",Context.MODE_PRIVATE);

 SharedPreferences.Editor editor = prefs.edit();

editor.putString("email", "[email protected]");

editor.putString("nombre", "Prueba");

editor.commit();
// hasta ahora has guardado un nombre y un email

String correo = prefs.getString("email", " texto por defecto");

String nombre = prefs.getString("nombre", " texto por defecto");
// asi obtienes lo que has guardado.

Note: it is very useful to save temporary data and even use values in various forms or activitys. But it does not accept complex data like objects or arrays.

You can also save what the user chooses in preferences and if you accept the purchase you save it in a database. You just have to get the juice out of the preferences, and these can also be deleted if the user deletes the order or cancels it.

    
answered by 26.10.2018 / 23:59
source
0

You can use any of the Storage options:

link

As an example Shared preferences:

Create a SharedPreferences with the desired name, for example "My_preferences", you must use this same to save the value:

final Context context = this;
    final SharedPreferences sharedPre = getSharedPreferences("Mis_preferencias", context.MODE_PRIVATE);

    //Guardando el dato en SharedPreferences
    //SharedPreferences sharpref = getPreferences(context.MODE_PRIVATE);
    //SharedPreferences.Editor editor = sharpref.edit();
    SharedPreferences.Editor editor = sharedPre.edit();
    editor.putString("codigo", codigo);
    editor.apply();

To obtain the values you must define what preference, in addition remember that you must use getSharedPreferences() :

//SharedPreferences sharpref = getActivity().getPreferences(getActivity().MODE_PRIVATE);
SharedPreferences sharpref = getActivity().getSharedPreferences("Mis_preferencias", getActivity().MODE_PRIVATE);
String codigo = sharpref.getString("codigo","No hay dato");
Toast.makeText(getActivity(), codigo, Toast.LENGTH_LONG).show();

Review this question:

Save SharedPreferences by assigning a key using getDefaultSharedPreferences ()

    
answered by 28.10.2018 в 20:46