Home > Blockchain >  How to save and load multiple arrays in android Java?
How to save and load multiple arrays in android Java?

Time:03-30

I'm very new to Android Dev, within this month, and I'm attempting to create a simple app to store 'medication' that a user needs to take each day. Currently, I have 7 String Arrays, one for each day of the week, all initialised with null values. They're fixed at 5 deep.

    String[] mondayMed = new String[5];
    String[] tuesdayMed = new String[5];
    String[] wednesdayMed = new String[5];
    String[] thursdayMed = new String[5];
    String[] fridayMed = new String[5];
    String[] saturdayMed = new String[5];
    String[] sundayMed = new String[5];

The user can change day, which changes which array gets referenced, and then chose which of the 5 medication slots they wish to save their medication in. This is all fine and works well. The Arrays get changed, the textViews are updated etc. etc. For example here is the method I've made to set the TextViews to the content of whichever array and medication slot.

 public void setMedication(int day, int medNum){

        TextView medText = null;

        if(medNum == 0){
            medText = findViewById(R.id.medication1);
        } else if (medNum == 1){
            medText = findViewById(R.id.medication2);
        } else if (medNum == 2){
            medText = findViewById(R.id.medication3);
        } else if (medNum == 3){
            medText = findViewById(R.id.medication4);
        } else if (medNum == 4){
            medText = findViewById(R.id.medication5);
        }


        String medication;
        if(day == 0){
            medication = mondayMed[medNum];
            medText.setText(medication);
        } else if (day == 1){
            medication = tuesdayMed[medNum];
            medText.setText(medication);
        } else if (day == 2) {
            medication = wednesdayMed[medNum];
            medText.setText(medication);
        } else if (day == 3) {
            medication = thursdayMed[medNum];
            medText.setText(medication);
        } else if (day == 4) {
            medication = fridayMed[medNum];
            medText.setText(medication);
        } else if (day == 5) {
            medication = saturdayMed[medNum];
            medText.setText(medication);
        } else if (day == 6) {
            medication = sundayMed[medNum];
            medText.setText(medication);
        }
    }

I'm aware this is far from ideal code, however for the time being I just want to get this working as simply as possible for myself to understand and develop further, later.

The Key thing I need now for this application to 'work' is a way to save these arrays when the application closes, or even simpler (I hear) when a button is pressed. And then, when the application is restarted, the saved data is read and the arrays are populated with said data. So that a user inputs their medication for the week, and then can open the app each day and see what meds they need to take.

I've done all of this within MainActivity, just for my own understanding and simplicity for my first app.

CodePudding user response:

You can use two methods

  1. SharedPrefrences (Easy one but more code)

  2. Room database(more complex)

CodePudding user response:

The answer given by @Manjeet is right. But, you can also save you lines of code and make it more optimised. Why don't you try this library? It is easy to implement and use. It also uses SharedPreferences in the background.

Implementation


  1. Add this to your settings.gradle file:
maven{
   url 'https://jitpack.io'
}
  1. Add this to your build.gradle file:
implementation 'com.github.sambhav2358:TinyDB:1.9'

Usage


It is easy to use also. Just do like this:

TinyDBManager tinyDB; //in your class

tinyDB = TinyDB.getInstance(context); // the context does not need be activity context

//Save value
tinyDB.putInt(key, value); // and a lot more formats to save

//get value
tinyDB.getInt(key, defaultValue);

You also get a option to get data listeners:

tinyDB.setValueChangeListener(new TinyDBManager.ValueChangeListener() {
            @Override
            public <E> void onValueAdded(String key, E value) {
                // a value is added or modified
            }

            @Override
            public void onKeyRemoved(String key) {
               // a value is removed
            }

            @Override
            public void onAllKeysRemoved() {
               // all the values are removed
            }
        });
  • Related