I have a doubt and I don't know how to solve it, if I have an ArrayList with only 3 positions with the values [1,2,3]. I would like that when the user adds for example the text [1], the data is saved in local mode in the device. And if later the user wants to add another text, for example [3].
At the end the SharedPreference would have saved the [1,3]. So if the user exits the app and re-enters, he can only redeem the text [2]. Because I already redeemed the [1,3].
I have the following, but I don't know how to continue correctly.
Promotional codes.
private ArrayList<String> codigoInvitacionAmigos;
codigoInvitacionAmigos.add("1");
codigoInvitacionAmigos.add("2");
codigoInvitacionAmigos.add("3");
Here is the button, for when the user clicks to redeem
btClaimInvite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("TODOS: ", codigoInvitacionAmigos.toString());
String codigo_temporal = inpt_Promo_code.getText().toString();
if(!codigo_temporal.isEmpty()){//comprobamos que NO este el campo vacío
for(int i=0; i < codigoInvitacionAmigos.size(); i++){
if(codigoInvitacionAmigos.get(i).contains(codigo_temporal)){
Toast.makeText(Bonus.this, "COINCIDENCIA", Toast.LENGTH_LONG).show();
//Guardar_CodigoReclamadoYCanjeado(0, codigo_temporal);
//codigoInvitacionAmigos.remove(codigo_temporal);
}else{
Toast.makeText(Bonus.this, "No coincide", Toast.LENGTH_LONG).show();
}
}
}
}
});
And here's how I'm trying to save it
private void Cargar_CodigoReclamadoYCanjeado(){
SharedPreferences load_CodigosReclamados = getSharedPreferences("CODIGO_CLAIM", MODE_PRIVATE);
String codi = load_CodigosReclamados.getString("codes_0", "");
if(!codi.isEmpty())codigoInvitacionAmigos.remove(codi);
Log.d("TODOS_Cargar", codi);
String codi_1 = load_CodigosReclamados.getString("codes_1", "");
if(!codi_1.isEmpty())codigoInvitacionAmigos.remove(codi_1);
Log.d("TODOS_Cargar", codi_1);
}
private void Guardar_CodigoReclamadoYCanjeado(int posicion, String codigoCanjeado){
SharedPreferences saved_CodigosReclamados = getSharedPreferences("CODIGO_CLAIM", MODE_PRIVATE);
SharedPreferences.Editor editor = saved_CodigosReclamados.edit();
switch (posicion){
case 1:
editor.putString("codes_1", codigoCanjeado);
break;
default:
editor.putString("codes_0", codigoCanjeado);
break;
}
editor.apply();
Log.d("TODOS_Saved", "Entra Saved");
}
Save used codes in shared preferences as a comma separated string (e.g. "1,3")
String[] codes = {"1", "2", "3"};
boolean isCodeValid(String code) {
for (String s : codes)
if (s.equals(code)) return true;
return false;
}
boolean isCodeUsed(String code) {
String usedCodes = sharedPreferences.getString("used_codes", "");
String[] usedCodesArray = usedCodes.split(",");
for (String s : usedCodesArray)
if (s.equals(code)) return true;
return false;
}
void setCodeUsed(String code) {
String usedCodes = sharedPreferences.getString("used_codes", "");
usedCodes += code + ",";
editor.putString("used_codes", usedCodes).apply();
}
Usage of functions :
String enteredCode = edtCode.getText().toString()
if (isCodeValid(enteredCode)) {
if (isCodeUsed(enteredCode)) System.out.println("Code already used");
else {
System.out.println("Code valid and not yet used");
setCodeUsed(enteredCode);
}
} else System.out.println("Code not valid");