2013-01-13 47 views
2

当我的应用程序启动时,它连接到一个数据库并下载一些XML格式的数据。这是布局:写一个ArrayList到SD卡

<Customers> 
    <Customer> 
    <name>...</name> 
    ... 
    </Customer> 
    <Customer> 
    <name>...</name> 
    ... 
    </Customer> 
    ... 
</Customer> 

对于我创建类Customer wicht我存储在一个ArrayList中的对象的每个客户。用户可以编辑和添加每个客户的一些数据,并可以将其上传回服务器。

问题是我不知道在本地存储数据的最佳方式是什么,所以如果我的应用程序关闭或用户没有互联网了,他们会有备份。我现在将所有Customer对象转换回XML,然后将其存储在SD卡上,但这不是我认为的好解决方案。每个客户都有大约40个字符串,10个整数和一些我必须存储的布尔值。有没有更好的方式在本地存储数据?

我发现some code这个可以存储一个ArrayList,但它不适用于自定义类。

编辑:我用this tutorial来解决我的问题。

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutput; 
import java.io.ObjectOutputStream; 
import java.util.ArrayList; 

import android.os.Environment; 

public class SerializeData { 
    public static byte[] serializeObject(Object o) { 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

     try { 
      ObjectOutput out = new ObjectOutputStream(bos); 
      out.writeObject(o); 
      out.close(); 

      // Get the bytes of the serialized object 
      byte[] buf = bos.toByteArray(); 

      return buf; 
     } catch (IOException ioe) { 
      CreateLog.addToLog(ioe.toString()); 
      return null; 
     } 
    } 

    public static Object deserializeObject(byte[] b) { 
     try { 
      ObjectInputStream in = new ObjectInputStream(
        new ByteArrayInputStream(b)); 
      Object object = in.readObject(); 
      in.close(); 

      return object; 
     } catch (ClassNotFoundException cnfe) { 
      CreateLog.addToLog(cnfe.toString()); 

      return null; 
     } catch (IOException ioe) { 
      CreateLog.addToLog(ioe.toString()); 

      return null; 
     } 
    } 

    public static void saveData(){ 
     byte[] arrayData = SerializeData 
       .serializeObject(MyTasks.allCustomers); 

     BufferedOutputStream bos; 
     try { 
      bos = new BufferedOutputStream(new FileOutputStream(
        Environment.getExternalStorageDirectory() 
          + "/myprogram/myarray.txt")); 
      bos.write(arrayData); 
      bos.flush(); 
      bos.close(); 
     } catch (FileNotFoundException e) { 
      CreateLog.addToLog(e.toString()); 
     } catch (IOException e) { 
      CreateLog.addToLog(e.toString()); 
     } 
    } 

    public static ArrayList<Customer> getData(){ 
     File afile = new File(Environment.getExternalStorageDirectory() 
       + "/myprogram/myarray.txt"); 
     int size = (int) afile.length(); 
     byte[] bytes = new byte[size]; 
     try { 
      BufferedInputStream buf = new BufferedInputStream(new FileInputStream(afile)); 
      buf.read(bytes, 0, bytes.length); 
      buf.close(); 
     } catch (FileNotFoundException e) { 
      CreateLog.addToLog(e.toString()); 
     } catch (IOException e) { 
      CreateLog.addToLog(e.toString()); 
     } 

     return (ArrayList<Customer>) SerializeData.deserializeObject(bytes); 

    } 
} 
+0

”有没有更好的方式在本地存储数据?“ - SQLite数据库将是我的第一选择。 – Squonk

回答

1

据我所知,客户结构只能在本地读取和写入,也可以通过相同或高度相关的程序进行读写。对于这种情况,我认为Java序列化是合适的,因为它只增加很少的代码。

让客户实现Serializable(立即放入serialVersionUID,以便以后可以控制版本)。 ArrayList已经是可序列化的。获取文件名,如here所述,并使用ObjectInput/Output流进行序列化。如果需要,您可以随后迁移到XML/JSON。一个简单的“hello world”序列化示例可以在here找到。 “

+0

非常感谢,我用这个代码:http://www.jondev.net/articles/Android_Serialization_Example_(Java) – ObAt