Android saves data in the ListView by storing it in a cached way

Source: Internet
Author: User

For such data:

<?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> Horse Gorgeous </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> The wedding is held in the beautiful Beihai Park in Beihai ... Nice and romantic place ... </content><pic>http://ss12.sinaimg.cn/wap240/473dae11494344debfc5b</pic><time >1288852274</time><source> MMS </source></mblog></rss>


First we take the data from the server, there is a main object mblog, we use an object to store:

public class MBlog implements <strong>Serializable</strong> {  //Ensure that this object is a private static final long that can be serialized 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 Str ing rtrootnick;public boolean rtrootvip;public String rtreason;public int rtnum;public int commentnum;public Date TIME;PU  Blic string Pic;public string Src;public string longitude;//longitude public string latitude;//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)) retur n true;} return false;} public int hashcode () {return Mblogid.hashcode () * 101 >> 12;}}

Path:mcachedir = This.getcachedir () in activity fetch to cache. GetPath ();

It's usually/data/data/com.example.weibotest/cache.


This is the Save method:

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) {}}


Read method:

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;}

To invoke this:

public void Parseassertdata () {InputStream was = 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 ();}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.