一、简介
存储List数据到本地的方式有很多种,对于不想用sqlite或者其他方式,又或是数据量很少的话,不妨可以试下用 SharedPreferences保存。由于SharedPreferences只能保存Map型的数据,必须要做其他转换才行。
二、应用场景
用于保存各种List数据,最常见的莫过于,ListView、Gridviw中的数据,支持类型有:
(1)List<String>
(2)List<Map<String,Object>>
(3)List<JavaBean>
三、项目实战
(1)、ListDataSave工具类,这里处理的方式时用gson把List转换成json类型,再利用SharedPreferences保存的。
gson的下载地址:http://download.csdn.net/detail/baiyulinlin1/9599507
- public class ListDataSave {
- private SharedPreferences preferences;
- private SharedPreferences.Editor editor;
- public ListDataSave(Context mContext, String preferenceName) {
- preferences = mContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
- editor = preferences.edit();
- }
- /**
- * 保存List
- * @param tag
- * @param datalist
- */
- public <T> void setDataList(String tag, List<T> datalist) {
- if (null == datalist || datalist.size() <= 0)
- return;
- Gson gson = new Gson();
- //转换成json数据,再保存
- String strJson = gson.toJson(datalist);
- editor.clear();
- editor.putString(tag, strJson);
- editor.commit();
- }
- /**
- * 获取List
- * @param tag
- * @return
- */
- public <T> List<T> getDataList(String tag) {
- List<T> datalist=new ArrayList<T>();
- String strJson = preferences.getString(tag, null);
- if (null == strJson) {
- return datalist;
- }
- Gson gson = new Gson();
- datalist = gson.fromJson(strJson, new TypeToken<List<T>>() {
- }.getType());
- return datalist;
- }
- }
- public class Userbean {
- private String name;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
(3)、MainActivity类
- public class MainActivity extends Activity implements OnClickListener {
- Context mContext;
- ListDataSave dataSave;
- private ArrayList<Userbean> listBean;
- private ArrayList<String> listString;
- private ArrayList<Map<String, Object>> listMap;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initView();
- }
- private void initView() {
- findViewById(R.id.btn_addString).setOnClickListener(this);
- findViewById(R.id.btn_addJavaBean).setOnClickListener(this);
- findViewById(R.id.btn_addMap).setOnClickListener(this);
- findViewById(R.id.btn_getString).setOnClickListener(this);
- findViewById(R.id.btn_getJavaBean).setOnClickListener(this);
- findViewById(R.id.btn_getMap).setOnClickListener(this);
- mContext = getApplicationContext();
- dataSave = new ListDataSave(mContext, "baiyu");
- listString = new ArrayList<String>();
- listBean = new ArrayList<Userbean>();
- listMap = new ArrayList<Map<String, Object>>();
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_addString:
- String s = "小名";
- listString.add(s);
- dataSave.setDataList("string", listString);
- break;
- case R.id.btn_getString:
- Toast.makeText(mContext, dataSave.getDataList("string").toString(), Toast.LENGTH_SHORT).show();
- break;
- case R.id.btn_addJavaBean:
- Userbean user = new Userbean();
- user.setName("小白");
- user.setAge(16);
- listBean.add(user);
- dataSave.setDataList("javaBean", listBean);
- break;
- case R.id.btn_getJavaBean:
- Toast.makeText(mContext, dataSave.getDataList("javaBean").toString(), Toast.LENGTH_SHORT).show();
- break;
- case R.id.btn_addMap:
- Map<String, Object> map=new HashMap<String, Object>();
- map.put("name","大白");
- map.put("age", 18);
- listMap.add(map);
- dataSave.setDataList("listMap", listMap);
- break;
- case R.id.btn_getMap:
- Toast.makeText(mContext, dataSave.getDataList("listMap").toString(), Toast.LENGTH_SHORT).show();
- break;
- }
- }
- }