2012-04-16 61 views
0

在我的应用程序中,我从服务器中检索记录。记录在一个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函数我的值的最佳方式是什么?我一直在看这么久,我认为我已经脑死亡了。 谢谢

+0

你为什么把结果放到Hashmap中?似乎它应该成为一个StudentRecord对象,当你解析它... – JRaymond 2012-04-16 21:49:03

+0

我最初把数据放入列表视图。但我想保存它,以便它可以在以后访问,我不想保存原始的XML,因为他们需要能够更新它并存储它 我不知道你的意思应该是什么成为学生记录对象 - 请你解释一下吗?谢谢 – Hank 2012-04-16 21:53:11

+0

我明白你为什么想要一个数据库;但为什么不能从XML创建一个studentRecord对象,然后将它传递给需要StudentRecord的数据库函数? – JRaymond 2012-04-16 21:54:51

回答

1

如果我读你的权利,这听起来像你需要从形式/活动将您传递addStudent功能,其中它现在的生活变成某种“助手”类:

private class dbHelper { 
    Database mDB; // set this up however you are doing it already 

    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(); 
    } 
    } 
} 

然后就是做给助手打电话时你解析你的XML

ArrayList<StudentRecord> StudentDownloads = new ArrayList<StudentRecord>(); 

for (int i = 0; i < nodes.getLength(); i++) {       
    Element e = (Element)nodes.item(i); 
    int id = Integer.valueOf(XMLfunctions.getValue(e, "StudentId"))); 
    int type = Integer.valueOf(XMLfunctions.getValue(e, "StudentType"))); 
    String location = XMLfunctions.getValue(e, "StudentLocation")); 
    String mother = XMLfunctions.getValue(e, "StudentMother")); 

    StudentRecord newRecord = new StudentRecord(id, type, location, mother); 

    StudentDownloads.add(newRecord); 
}   

然后就大功告成了处理时:

for (StudentRecord s : StudentDownloads) { 
    mDBHelper.addStudent(s); 
} 
+0

这就是我所做的。再次感谢我欣赏它 – Hank 2012-04-16 22:16:11