0

Related: Save ArrayList to SharedPreferences

I have an edit text to enter a value, a button to add it to an ArrayList and save, a button to retrieve and display the value, and a button to reset the ArrayList. But the button to collect constantly displays 2.

public class test extends AppCompatActivity {

    ArrayList arrayList = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);


        Button button = findViewById(R.id.create);
        Button button1 = findViewById(R.id.recup);
        Button button2 = findViewById(R.id.reset);
        final EditText editText = findViewById(R.id.editText);
        final TextView textView = findViewById(R.id.textView);

        //Initialize ArrayList
        loadData();

        //Button for add variable in arraylist and save
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nombre = editText.getText().toString();
                arrayList.add(nombre);

                saveData();

            }
        });

        //Button for look arrayList.get(0)
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText(String.valueOf(arrayList.get(0)));
            }
        });

        //Button for reset
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("shared preferencs", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.remove("task list");
                editor.commit();
            }
        });
    }

    //save data
    private void saveData() {
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferencs", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(arrayList);
        editor.putString("task list", json);
        editor.apply();
    }

    //load data
    private void loadData() {
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferencs", MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString("task list", null);
        Type type = new TypeToken<ArrayList>() {
        }.getType();
        arrayList = gson.fromJson(json, type);

        }
    }
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
Leic
  • 57
  • 8
  • Can you please explain why you're only getting the 0 index of the list? – jbmcle Jan 15 '19 at 23:17
  • I want to recover just the first entry for the moment. – Leic Jan 15 '19 at 23:25
  • There was an issue with saving data, around API 20, Android 5. Later devices are unable store/retrieve the data. I only know of this from the end user standpoint. –  Jan 16 '19 at 02:56
  • I am obliged to save value by value or create database... – Leic Jan 16 '19 at 12:13

0 Answers0