设为首页 加入收藏

TOP

两个可能会经常用到的简便方法
2017-10-13 10:36:56 】 浏览:673
Tags:两个 可能 经常 用到 简便 方法
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, …)

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇android下载管理、理财、浏览器、.. 下一篇Android Gradle使用总结

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目