0

Before this i just used string and integer only to set preferences. But now, i want set array. For example i want set value for index 0. setTotalValue[0] = 90; . Then i can get back using getTotalValue[0], so i will get value 90. Before this i used string , it much simple to me. This i show my code i make preference for string.

This class object

public class ShowDateFromTo {
private String fromDate;

public ShowDateFromTo(String fromDate) {
    this.fromDate = fromDate;
}

public String getFromDate() {
    return fromDate;
}

public void setFromDate(String fromDate) {
    this.fromDate = fromDate;
}

}

I change it to this one,

public class TotalBadge {
private ArrayList<Integer> totalValue = new ArrayList<Integer>();

public TotalBadge(ArrayList totalValue){
    this.totalValue = totalValue;
}

public ArrayList<Integer> getTotalValue() {
    return totalValue;
}

public void setTotalValue(ArrayList<Integer> totalValue) {
    this.totalValue = totalValue;
}

}

Am i right ?

This class Preferences

public class PreferencesFromToDate {

SharedPreferences preferences;
public PreferencesFromToDate(Context context){
    preferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
}

public void setPreferencesFromToDate(ShowDateFromTo profiles){
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("FROMDATE",profiles.getFromDate());
    editor.apply();
}

public ShowDateFromTo getPreferencesFromToDate(){
    String fromDate = preferences.getString("FROMDATE", "");
    ShowDateFromTo profiles = new ShowDateFromTo(fromDate);

    profiles.setFromDate(fromDate);
    return profiles;
}
}

This show i set it

ShowDateFromTo profile = new ShowDateFromTo(newdateFrom);
profile.setFromDate(newdateFrom);

How can i replace string with array. I have 4 index of array. Mean totalValue[0],totalValue[1],totalValue[2],totalValue[3]

MAS. John
  • 582
  • 6
  • 22

3 Answers3

0

You can use json format to store your data.

public void setPreferencesFromToDate(ShowDateFromTo profiles){
SharedPreferences.Editor editor = preferences.edit();
editor.putString("FROMDATE", gson.toJson(profiles));
editor.apply();
}

public ShowDateFromTo getPreferencesFromToDate(){
ShowDateFromTo fromDate = gson.fromJson(preferences.getString("FROMDATE", ""), ShowDateFromTo.class);
return fromDate;
}

Don't forget to add gson to your gradle file:

compile 'com.google.code.gson:gson:2.7'

Use json is popular for professional android developer. you should know well it.

http://androidopentutorials.com/android-sharedpreferences-tutorial-and-example/

Luong Dinh
  • 569
  • 4
  • 17
0

You can store array as JSON String. Whenever required, you can retrieve the JSON String and parse into array. You can check this link

Community
  • 1
  • 1
Gr8Warrior
  • 699
  • 7
  • 11
0

You can do something like this. Store data with different key names for each item.

public class PreferencesData {

    private static final String PREF_NAME   = "pref";
    private static final String KEY_DATA    = "key_data_";

    private SharedPreferences mPreferences;
    private int mSize;

    public PreferencesData(Context context, int size) {
        mSize = size;
        mPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public ArrayList<Integer> getArrayValue() {
        ArrayList<Integer> result = new ArrayList<>();
        for (int i = 0; i < mSize; i++) {
            result.add(i, getValue(i));
        }
        return result;
    }

    public Integer getValue(int index){
        return mPreferences.getInt(KEY_DATA + index, 0);
    }

    public void setValue(int index, int value) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putInt(KEY_DATA + index, value);
        editor.apply();
    }
}

Usage:

// Set initial size of 4
PreferencesData preferences = new PreferencesData(this, 4);
// set value of 100 to index 0
preferences.setValue(0, 100);
// set value of 20 to index 1
preferences.setValue(1, 20);
// set value of 40 to index 3
preferences.setValue(3, 40);

// Get all data
ArrayList<Integer> arrayList = preferences.getArrayValue();
for (int i = 0; i < arrayList.size(); i++) {
   ...
}

// Get value of index 1
int value = preferences.getValue(1);
noahutz
  • 1,207
  • 1
  • 9
  • 11