用SharedPreferences、Gson分别来保存List、Map数据

package ysl.savelistMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;
import org.json.JSONTokener;

import com.google.gson.Gson;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView tv_save,tv_get;
	private SharedPreferences sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv_save = (TextView) findViewById(R.id.tv_save);
		tv_get = (TextView) findViewById(R.id.tv_get);
		sp=getSharedPreferences("mysp", MODE_PRIVATE);
		Gson gson = new Gson();
		
		List<String> nameList = new  ArrayList<String>();
		for (int i = 0; i < 3; i++) {
			nameList.add("y");
			nameList.add("s");
			nameList.add("l");
		}
		spSaveGetList(nameList);//sp直接存储list
		gsonSaveGetList(gson, nameList);//gson存储list
		saveList(nameList);
		getList();
		
		Map<Integer, String> age2nameMap=new HashMap<Integer, String>();
		age2nameMap.put(21, "we");
		age2nameMap.put(25, "qa");
		age2nameMap.put(23, "gh");
		spSaveGetMap(age2nameMap);//sp直接存储map
		gsonSaveGetMap(gson, age2nameMap);//gson存储map
		saveMap(age2nameMap);
		getMap();
	}

	private void gsonSaveGetMap(Gson gson, Map<Integer, String> age2nameMap) {
		sp.edit().putString("mmmMap", gson.toJson(age2nameMap)).commit();
		Map jsonMap = gson.fromJson(sp.getString("mmmMap", null), Map.class);
		System.out.println("toJson:"+jsonMap);
	}

	private void spSaveGetMap(Map<Integer, String> age2nameMap) {
		sp.edit().putString("mmMap", age2nameMap.toString()).commit();
		String mapResult = sp.getString("mmMap", "");
		System.out.println("toString:"+mapResult);
	}

	private Map<Integer , String> getMap() {
		Map< Integer, String> mage2nameMap = new HashMap<>();
		String age2name = sp.getString("mMap", null);
		if (age2name.length() > 0) {
			JSONTokener jsonTokener = new JSONTokener(age2name);
			try {
				JSONArray  jsonArray  = (JSONArray) jsonTokener.nextValue();
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					mage2nameMap.put(jsonObject.getInt("age"), jsonObject.getString("name"));
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		tv_get.setText(mage2nameMap.toString());
		return mage2nameMap;
	}

	private void saveMap(Map<Integer, String> age2nameMap) {
		if (age2nameMap!=null) {
			JSONStringer jsonStringer = new JSONStringer();
			try {
				jsonStringer.array();
				for (Integer integer : age2nameMap.keySet()) {
					jsonStringer.object();
					jsonStringer.key("age");
					jsonStringer.value(integer);
					jsonStringer.key("name");
					jsonStringer.value(age2nameMap.get(integer));
					jsonStringer.endObject();
				}
				jsonStringer.endArray();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			tv_save.setText(jsonStringer.toString());
			sp.edit().putString("mMap", jsonStringer.toString()).commit();
			System.out.println("jsonStringer"+jsonStringer);
		}
	}
	
	private void gsonSaveGetList(Gson gson, List<String> nameList) {
		sp.edit().putString("mmmList", gson.toJson(nameList)).commit();
		List jsonList = gson.fromJson(sp.getString("mmmList", null), List.class);
		System.out.println("toJson:"+jsonList);
	}

	private void spSaveGetList(List<String> nameList) {
		sp.edit().putString("mmList", nameList.toString()).commit();
		String mmnameResult = sp.getString("mmList", "");
		System.out.println("toString:"+mmnameResult);
	}

	private List<String> getList() {
		List<String> result = new ArrayList<String>();
		String nameResult = sp.getString("mList", "");
		
		if (nameResult.length() > 0) {
			JSONTokener jsonTokener = new JSONTokener(nameResult);
			try {
				JSONArray jsonArray = (JSONArray) jsonTokener.nextValue();
				for (int i = 0; i < jsonArray.length(); i++) {
					String name = (String) jsonArray.get(i);
					result.add(name);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		tv_get.setText(result.toString());
		return result;
	}

	private void saveList(List<String> nameList) {
		if (nameList!=null) {
			JSONStringer jsonStringer = new JSONStringer();
			try {
				jsonStringer.array();
				for (String string : nameList) {
					jsonStringer.value(string);
				}
				jsonStringer.endArray();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			tv_save.setText( jsonStringer.toString());
			sp.edit().putString("mList", jsonStringer.toString()).commit();
			System.out.println("jsonStringer"+jsonStringer);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值