Android 将大型ArrayList存储到SQLite

Android 将大型ArrayList存储到SQLite,android,sqlite,serialization,Android,Sqlite,Serialization,我正在开发一个应用程序,不断下载大量数据 数据是json格式的,我使用Gson对其进行反序列化。现在我把它存储在SQLite数据库中。我必须计算关系并将它们存储到关系数据库中。然而,这需要太多的时间 将类对象保存到db会更方便 反序列化后的数据如下所示: Class A{ private String name; private List<B> subItems; } Class B{ private String name; private List<C> subIt

我正在开发一个应用程序,不断下载大量数据

数据是json格式的,我使用Gson对其进行反序列化。现在我把它存储在SQLite数据库中。我必须计算关系并将它们存储到关系数据库中。然而,这需要太多的时间

将类对象保存到db会更方便

反序列化后的数据如下所示:

Class A{
private String name;
private List<B> subItems;
}

Class B{
private String name;
private List<C> subItems
}
A类{
私有字符串名称;
私人清单分项;
}
B类{
私有字符串名称;
专用列表子项
}

我可以用一种更简单的方法来保持它,例如使它们可序列化吗?如何做到这一点

是的,序列化在您的情况下是一个更好的解决方案,它可以在Android中工作。以下示例取自

序列化方法

  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) { 
      Log.e("serializeObject", "error", ioe); 

      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) { 
      Log.e("deserializeObject", "class not found error", cnfe); 

      return null; 
    } catch(IOException ioe) { 
      Log.e("deserializeObject", "io error", ioe); 

      return null;
  } 
反序列化方法

  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) { 
      Log.e("serializeObject", "error", ioe); 

      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) { 
      Log.e("deserializeObject", "class not found error", cnfe); 

      return null; 
    } catch(IOException ioe) { 
      Log.e("deserializeObject", "io error", ioe); 

      return null;
  } 

谢谢!现在您知道我如何处理类之间的关系了吗。类A拥有一个包含类B的列表。我是序列化每个类还是只序列化“top”类?使这两个类都可序列化。我想你只是序列化了类A。我将它们保存为一个blob?Sqlite中没有
blob
,但我认为它应该可以工作。检查:,2.1.3了解详细信息。您还可以保存到磁盘,我指出的示例显示了.BTW!!!!它不会影响性能吗。如果是json。为什么不使用json.ToString()函数并将其存储到数据库中呢。稍后使用JSONParser,您可以将字符串转换为JSON。