两个可能会常常用到的简便方法

两个可能会经常用到的简便方法
1、用SharedPreferences保存List
 1 private final String spName = BuildConfig.APPLICATION_ID + BuildConfig.VERSION_CODE;
 2 public List<T> getList() {
 3  SharedPreferences sp = getSharedPreferences(spName, Context.MODE_PRIVATE);
 4  List<T> list = new ArrayList<>();
 5  String strJson = sp.getString(TAG, null);
 6  if (null == strJson) {
 7    return list;
 8  }
 9  Gson gson = new Gson();
10  list = gson.fromJson(strJson, new TypeToken<List<T>>() {
11  }.getType());
12  return list;
13}
14 
15public void setList(List<T> list) {
16  if (list == null || list.size() == 0) {
17    return;
18  }
19  SharedPreferences sp = getSharedPreferences(spName, Context.MODE_PRIVATE);
20  SharedPreferences.Editor editor = sp.edit();
21  Gson gson = new Gson();
22  String strJson = gson.toJson(list);
23  editor.clear();
24  editor.putString(TAG, strJson);
25  editor.apply();
26}

2、TextView设置内容

public static String getAppString(int resId, Object... args) {
        if (resId == 0) {
            return " ";
        }
        try {
            CharSequence text = getInstance().getResources().getString(resId, args);
            if (!TextUtils.isEmpty(text)) {
                return text.toString();
            } else {
                return " ";
            }
        } catch (Exception e) {
            return " ";
        }
    }

使用方式:

textView.setText(App.getAppString(R.string.text, string1, string2, …)