zoukankan      html  css  js  c++  java
  • android用存到缓存的方法来保存ListView里的数据

    对于这种数据:

    <?

    xml version="1.0" encoding="utf-8" ?> <rss><sid>77f265bb46de068e78f35afbadec62af</sid><count>3</count><control>0</control> <mblog><uid>1195224593</uid><favid>3436952795</favid><mblogid>5xtaJR</mblogid><mblogidnum>3436952795</mblogidnum><mblogtype>0</mblogtype><mlevel>0</mlevel><feedid>5xtaJR</feedid><nick>马艳丽</nick><portrait>http://tp2.sinaimg.cn/1195224593/50/5614100014/0</portrait><vip>1</vip><vipsubtype>0</vipsubtype><member_type>13</member_type><remark></remark><level>2</level><rtnum>11</rtnum><commentnum>25</commentnum><attitudenum>0</attitudenum><attitudeid>0</attitudeid><attitudes_status>0</attitudes_status><attitudes_count>0</attitudes_count><mblogtypename></mblogtypename><visible><type>0</type><list_id>0</list_id></visible><content>婚礼在北海漂亮的北海公园举行…好美好浪漫的地方… </content><pic>http://ss12.sinaimg.cn/wap240/473dae11494344debfc5b</pic><time>1288852274</time><source>彩信</source></mblog></rss>



    首先我们把从server取到的数据,里面有个基本的对象mblog,我们用一个对象来存储:

    public class MBlog implements <strong>Serializable</strong> {  //保证这个对象是能够序列化的
    	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<MBlog> list = (List<MBlog>)array[1];
    				FileUtils.save(list, mCacheDir+'/'+"001_fav");
    				
    				
    				List<MBlog> list1 = (List<MBlog>)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();
    		}
    	}


  • 相关阅读:
    剑指offer 面试26题
    剑指offer 面试25题
    剑指offer 面试24题
    剑指offer 面试23题
    剑指offer 面试22题
    剑指offer 面试19题
    剑指offer 面试20题
    剑指offer 面试21题
    剑指offer 面试18题
    剑指offer 面试17题
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6941385.html
Copyright © 2011-2022 走看看