在我的应用程序中,我从服务器中检索记录。记录在一个xml文件中。我能够下载文件并解析它,没有任何问题。我将结果存储在HashMap中,我希望能够将这些结果放入我的应用程序的SQLite数据库中。从HashMap获取数据到SQLite数据库
下面是HashMap中
ArrayList<HashMap<String, String>> StudentDownloads = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getXML(target);
Document doc = XMLfunctions.XMLfromString(xml);
if(XMLfunctions.XMLfromString(xml)==null){
Toast.makeText(this, "Badly Formed File", Toast.LENGTH_LONG).show();
finish();
}
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(this, "There Were No Student Results To Show", Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("Students");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("Student Id", "Student Id " + XMLfunctions.getValue(e, "StudentId"));
map.put("Student Type", "Student Type" + XMLfunctions.getValue(e, "StudentType"));
map.put("Student Location", "Student Location" + XMLfunctions.getValue(e, "StudentLocation"));
map.put("Student Mother", "Student Mother" + XMLfunctions.getValue(e, "StudentMother"));
StudentDownloads.add(map);}
};
现在,在我的应用程序的代码,我已经创建了一个使用了一个名为StudentRecord类,在我输入表单数据输入表单我使用这个功能来更新文件
private void addStudent(StudentRecord newRecord){
mDB.beginTransaction();
try {
ContentValues StudentRecordToAdd = new ContentValues();
StudentRecordToAdd.put(Students.STUDENT_ID, newRecord.getStudentName());
StudentRecordToAdd.put(Student.STUDENT_TYPE, newRecord.getStudentType());
StudentRecordToAdd.put(Student.STUDENT_LOCATION, newRecord.getStudentLocation());
StudentRecordToAdd.put(Student.STUDENT_MOTHER, newRecord.getStudentMother());
mDB.insert(Student.STUDENT_TABLE_NAME,Student.STUDENT_ANIMALID, StudentRecordToAdd);
mDB.setTransactionSuccessful();
Toast.makeText(this,"Recorded Added ",0).show();
} finally {
mDB.endTransaction();
}
什么是从我的值到我的NewRecord函数我的值的最佳方式是什么?我一直在看这么久,我认为我已经脑死亡了。 谢谢
你为什么把结果放到Hashmap中?似乎它应该成为一个StudentRecord对象,当你解析它... – JRaymond 2012-04-16 21:49:03
我最初把数据放入列表视图。但我想保存它,以便它可以在以后访问,我不想保存原始的XML,因为他们需要能够更新它并存储它 我不知道你的意思应该是什么成为学生记录对象 - 请你解释一下吗?谢谢 – Hank 2012-04-16 21:53:11
我明白你为什么想要一个数据库;但为什么不能从XML创建一个studentRecord对象,然后将它传递给需要StudentRecord的数据库函数? – JRaymond 2012-04-16 21:54:51