Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android用存到緩存的方法來保存ListView裡的數據

android用存到緩存的方法來保存ListView裡的數據

編輯:關於Android編程

對於這樣的數據:


77f265bb46de068e78f35afbadec62af30
119522459334369527955xtaJR3436952795005xtaJR馬艷麗http://tp2.sinaimg.cn/1195224593/50/5614100014/0101321125000000婚禮在北海美麗的北海公園舉行…好美好浪漫的地方… http://ss12.sinaimg.cn/wap240/473dae11494344debfc5b彩信


首先我們把從服務器取到的數據,裡面有個主要的對象mblog,我們用一個對象來存儲:

public class MBlog implements Serializable {  //保證這個對象是可以序列化的
	private static final long serialVersionUID = -3514924369786543050L;
	public String uid;
	public String favid;
	public String mblogid;
	public String nick;
	public String portrait;
	public boolean vip;
	public String content;
	public String rtrootuid;
	public String rtrootid;
	public String rtrootnick;
	public boolean rtrootvip;
	public String rtreason;
	public int rtnum;
	public int commentnum;
	public Date time;
	public String pic;
	public String src;
	public String longitude;// 經度
	public String latitude;// 緯度

	public boolean equals(Object o) {
		if (o == null) return false;
		if (o == this) return true;
		Class cla = o.getClass();
		if (cla == getClass()) {
			MBlog other = (MBlog) o;
			if (other.mblogid.equals(mblogid)) return true;
		}
		return false;
	}

	public int hashCode() {
		return mblogid.hashCode() * 101 >> 12;
	}


}

在Activity取到緩存的Path: mCacheDir = this.getCacheDir().getPath();

一般是/data/data/com.example.weibotest/cache


這個是save方法:

	public static void save(Object obj, String path) {
		try {
			File f = new File(path);
			/*if(f != null){
				f.mkdirs();
				f.createNewFile();
			}*/
			FileOutputStream fos = new FileOutputStream(f);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(obj);
			oos.flush();
			oos.close();
		}
		catch (IOException e) {
		}
	}


讀取方法:

	public static Object load(String path) {
		Object obj = null;
		File file = new File(path);
		try {
			/*if(file != null){
				file.mkdirs();
			}*/
			if (file.exists()) {
				FileInputStream fis = new FileInputStream(file);
				ObjectInputStream ois = new ObjectInputStream(fis);
				try {
					obj = ois.readObject();
				}
				catch (ClassNotFoundException e) {
				}
				ois.close();
			}	
		}catch (IOException e) {
			}
		return obj;
	}

這樣來調用:

public void parseAssertData() {
		InputStream is = null;
		try {
			is = this.getAssets().open("11.xml", Context.MODE_PRIVATE);
			int length = is.available();
			byte[] buffer = new byte[length];
			is.read(buffer);
			String temp = new String(buffer);

			try {
				Object[] array = ParseData.getMBlogList(temp);
				List list = (List)array[1];
				FileUtils.save(list, mCacheDir+'/'+"001_fav");
				
				
				List list1 = (List)FileUtils.load(mCacheDir+'/'+"001_fav");
				MBlog blog = list1.get(1);
				System.out.println("===size="+blog.src);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved