Android 如何将自定义arraylist存储到sqlite中并检索它
我需要存储自定义arraylist int sqlite db并检索它 am使用的arrylist是Android 如何将自定义arraylist存储到sqlite中并检索它,android,arraylist,android-sqlite,Android,Arraylist,Android Sqlite,我需要存储自定义arraylist int sqlite db并检索它 am使用的arrylist是 List<LessonContent> list = new ArrayList<LessonContent>(); 我用这种方式将它存储在列表中 LessonContent textobj = new LessonContent(tagname,type,content); list.add(text
List<LessonContent> list = new ArrayList<LessonContent>();
我用这种方式将它存储在列表中
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");
Lessons lsn=db.getContent(lesson_id);
字符串xmlcontent=lsn.getLesson();
JSONObject json=null;
json=新的JSONObject(xmlcontent);
list=(list)json.optJSONArray(“uniqueArrays”);
我在此阶段遇到错误,因为无法将json对象强制转换为列表
请任何人知道在db中存储我的arraylist的正确方法并将其读回。您不能将JSONArray强制转换为列表。您必须解析json数组并构建列表还有其他方法来存储和检索我的arraylist吗
Lessons lsn = db.getContent(lesson_id);
String xmlcontent = lsn.getLesson();
JSONObject json = null;
json = new JSONObject(xmlcontent);
list = (List<LessonContent>) json.optJSONArray("uniqueArrays");