Android SharedPreferences Save updatePrefValueByModel(SharedPreferences pref, String key, String thisModel, String targetModel, Object targetModelValue, Object notTargetModelValue)

Here you can find the source of updatePrefValueByModel(SharedPreferences pref, String key, String thisModel, String targetModel, Object targetModelValue, Object notTargetModelValue)

Description

update Pref Value By Model

Declaration

public static Object updatePrefValueByModel(SharedPreferences pref,
            String key, String thisModel, String targetModel,
            Object targetModelValue, Object notTargetModelValue) 

Method Source Code

//package com.java2s;
import java.util.Locale;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Main {
    public static Object updatePrefValueByModel(SharedPreferences pref,
            String key, String thisModel, String targetModel,
            Object targetModelValue, Object notTargetModelValue) {

        Object value;/*from   www  .  j a v  a2 s  .co  m*/
        String mdl = "#" + thisModel.toUpperCase(Locale.US) + "#";
        if (targetModel != null && targetModel.indexOf(mdl) != -1) {
            value = targetModelValue;
        } else {
            value = notTargetModelValue;
        }

        if (value != null) {
            Editor edit = pref.edit();
            if (value instanceof String) {
                edit.putString(key, (String) value);
            } else if (value instanceof Boolean) {
                edit.putBoolean(key, (Boolean) value);
            }
            edit.commit();
        }

        return value;
    }

    public static int indexOf(String[] datas, String val) {
        if (val != null) {
            for (int i = 0; i < datas.length; i++) {
                if (datas[i].equals(val)) {
                    return i;
                }
            }
        }
        return -1;
    }
}

Related

  1. putPrefs(Activity context, int mode, Map pairs)
  2. putSharedPrefs(Context context, String prefName, int mode, Map pairs)
  3. saveSetting(Context context, String name, Long value)
  4. saveSetting(Context context, String name, String value)
  5. setSharedPreferences(String key, String value, Context context)
  6. putPrefBoolean(Context c, String key, boolean value)
  7. putPrefInt(Context c, String key, int value)