在我的应用程序中,我正在从服务器检索记录。记录在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函数的最佳方法是什么?我看了这么久,我想我已经死了。
谢谢

最佳答案

如果我没看错,听起来您需要将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);
}

10-07 19:34
查看更多