如何将自定义arraylist存储到sqlite并检索它

时间:2013-06-25 14:51:40

标签: android arraylist android-sqlite

我需要存储我的自定义arraylist int sqlite db并检索它。

使用的arrylist是

List<LessonContent> list = new ArrayList<LessonContent>(); 

lessonContent是一个带

的对象
 public LessonContent(String n, String a, String c) {
        node = n;
        attribute = a;
        content = c;

    }

我将它以这种方式存储在列表中..

LessonContent textobj = new LessonContent(tagname,type,content);
                                 list.add(textobj);

并且我以这种方式将它存储在sqlite中..因为我读了一些其他问题,我们无法将arraylist直接存储到sqlite中,因此将其转换为json字符串。

// saving list to sqlite db
    JSONObject json = new JSONObject();

json.put("uniqueArrays", new JSONArray(list));
String arrayList = json.toString();

    Lessons lns =new Lessons(lesson_id,subject,chapter,arrayList);
    db.addContent(lns);     

对此代码没有任何问题......

现在我需要从sqlite中读取存储的数据..

因为我使用这段代码..

Lessons lsn = db.getContent(lesson_id);
String xmlcontent = lsn.getLesson();
      JSONObject json = null;

json = new JSONObject(xmlcontent);

list = (List<LessonContent>) json.optJSONArray("uniqueArrays");

我在这个阶段出错,因为无法将json对象强制转换为列表..

请任何人知道将我的arraylist存储在db中的正确方法并将其读回..

0 个答案:

没有答案