0

在我的应用程序中,我正在从服务器检索记录。记录在一个 xml 文件中。我能够毫无问题地获取文件并解析它。我将结果存储在 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();
             }

将我的值从 HashMap 获取到我的 NewRecord 函数的最佳方法是什么?我看了这么久,我想我已经脑死了。谢谢

4

1 回答 1

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);
}
于 2012-04-16T22:10:57.977 回答