'How to save and load multiple arrays in android Java?
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.
Solution 1:[1]
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
- Add this to your
settings.gradle
file:
maven{
url 'https://jitpack.io'
}
- Add this to your
build.gradle
file:
implementation 'com.github.sambhav2358:TinyDB:2.0.1'
Usage
It is easy to use also. Just do like this:
TinyDefaultDB tinyDB; //in your class
tinyDB = TinyDB.getInstance().getDefaultDatabase(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: This feature is not present in shared preferences
tinyDB.setValueChangeListener(new TinyDBManager.ValueChangeListener() {
@Override
public <E> void onValueAdded(String key, E value, String dbName) {
// a value is added or modified
}
@Override
public void onKeyRemoved(String key, String dbName) {
// a value is removed
}
@Override
public void onAllKeysRemoved(String dbName) {
// all the values are removed
}
});
Solution 2:[2]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Manjeet deswal |