I am designing a shopping cart on android, and I would like to know how to store the data it stores. or what ideas can you give me to save this data in a database. Because if I only save the variables, only enter the last element shown
public class Cesta extends AppCompatActivity {
ArrayList<Elemento> cesta = new ArrayList<>();
SharedPreferences carrito;
Gson gson = new Gson();
TextView mos;
Button btnOrdenar;
double total =0;
int cant=0;
String producto;
double montobd=0;
double precio=0;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_cesta);
carrito=getSharedPreferences("carrito",MODE_PRIVATE);
String guardado = carrito.getString("cesta","");
////gson convierte el elemento guardado a un arraylist
Type type = new TypeToken<ArrayList<Elemento>>(){}.getType();
cesta=gson.fromJson(guardado,type);
btnOrdenar = (Button)findViewById(R.id.btnOrdenar);
btnOrdenar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(total==0|producto==""|montobd==0){
Toast.makeText(getApplicationContext(),"El carrito está vacío ",Toast.LENGTH_SHORT).show();
}else{
Intent i = new Intent(getApplicationContext(), Compras.class);
i.putExtra("total",total);
i.putExtra("cantidad",cant);
i.putExtra("monto",montobd);
i.putExtra("nombre",producto);
startActivity(i);
}
}
});
cuerpoCesta();
}
@SuppressLint("WrongViewCast")
public void cuerpoCesta(){
final LinearLayout ll = (LinearLayout) findViewById(R.id.llCesta);
ll.removeAllViews();
LinearLayout linearLayout=null;
CardView cardView=null;
DecimalFormat formato = new DecimalFormat("$0.00");
total=0;
cant = 0;
montobd = 0;
precio=0;
for (int i=0;i<cesta.size();i++){
Elemento E= cesta.get(i);
cardView = new CardView(ll.getContext());
linearLayout = new LinearLayout(this);
linearLayout.setOrientation((LinearLayout.HORIZONTAL));
linearLayout.setPadding(10,10,10,10);
cardView.setPadding(10,10,10,10);
cardView.setContentPadding(10,10,10,10);
cardView.setCardElevation(10);
final TextView txtPro=new TextView(this);
txtPro.setText(E.getNombre());
txtPro.setLayoutParams(new LinearLayout.LayoutParams(450, ViewGroup.LayoutParams.WRAP_CONTENT));
final TextView txtPre=new TextView(this);
txtPre.setText("$"+E.getPrecio()+"");
txtPre.setLayoutParams(new LinearLayout.LayoutParams(330, ViewGroup.LayoutParams.WRAP_CONTENT));
final TextView txtCan=new TextView(this);
txtCan.setText(E.getCantidad()+"");
txtCan.setLayoutParams(new LinearLayout.LayoutParams(200, ViewGroup.LayoutParams.WRAP_CONTENT));
final TextView txtMonto=new TextView(this);
double montoCompra=E.getPrecio()*E.getCantidad();
formato.format(montoCompra);
txtMonto.setText("$"+montoCompra+"");
txtMonto.setLayoutParams(new LinearLayout.LayoutParams(200, ViewGroup.LayoutParams.WRAP_CONTENT));
final Button btnRestar= new Button(this);
btnRestar.setText("X");
btnRestar.setLayoutParams(new LinearLayout.LayoutParams(150, ViewGroup.LayoutParams.WRAP_CONTENT));
final String cod=cesta.get(i).getCodigo();
btnRestar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i=0; i<cesta.size();i++){
if (cesta.get(i).getCodigo().equalsIgnoreCase(cod)){
cesta.get(i).setCantidad(cesta.get(i).getCantidad()-1);
if (cesta.get(i).getCantidad()<1)
cesta.remove(i);
}
}
String jsonList = gson.toJson(cesta);
carrito = getSharedPreferences("carrito",MODE_PRIVATE);
SharedPreferences.Editor editor = carrito.edit();
editor.putString("cesta",jsonList);
//commin es guardar datos y cerrar preferencias
editor.commit();
cuerpoCesta();
}
});
linearLayout.addView(txtPro);
linearLayout.addView(txtPre);
linearLayout.addView(txtCan);
linearLayout.addView(txtMonto);
linearLayout.addView(btnRestar);
cardView.addView(linearLayout);
ll.addView(cardView);
formato.format(total+=montoCompra);
cant=E.getCantidad();
producto = E.getNombre();
montobd = montoCompra;
precio = E.getPrecio();
}
final TextView txtTextoTotal = new TextView(this);
txtTextoTotal.setText("Total a apagar");
txtTextoTotal.setLayoutParams(new LinearLayout.LayoutParams(800, ViewGroup.LayoutParams.WRAP_CONTENT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
txtTextoTotal.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
txtTextoTotal.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
}
final TextView txtTotal = new TextView(this);
txtTotal.setText(formato.format(total));
txtTotal.setLayoutParams(new LinearLayout.LayoutParams(280, ViewGroup.LayoutParams.WRAP_CONTENT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
txtTotal.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
}
LinearLayout linearLayout2 = new LinearLayout(this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
linearLayout2.addView(txtTextoTotal);
linearLayout2.addView(txtTotal);
ll.addView(linearLayout2);
}
}