Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从HashMap获取数据到SQLite数据库_Java_Android_Sqlite - Fatal编程技术网

Java 从HashMap获取数据到SQLite数据库

Java 从HashMap获取数据到SQLite数据库,java,android,sqlite,Java,Android,Sqlite,在我的应用程序中,我正在从服务器检索记录。这些记录位于xml文件中。我可以毫不费力地下载文件并解析它。我将结果存储在HashMap中,我希望能够将这些结果放入我的应用程序的SQLite数据库中 下面是HashMap的代码 ArrayList<HashMap<String, String>> StudentDownloads = new ArrayList<HashMap<String, String>>();

在我的应用程序中,我正在从服务器检索记录。这些记录位于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);}         
        };
从HashMap到NewRecord函数获取值的最佳方法是什么?我看了这么久,我想我已经脑死亡了。
谢谢

如果我读对了,听起来您需要将addStudent函数从它现在所在的表单/活动移动到某种“助手”类中:

然后在解析完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);
}         

为什么要将结果放入Hashmap?似乎在你解析它的那一刻,它就应该成为一个StudentRecord对象……我最初是将数据放入listview的。但是我想保存它,以便以后可以访问它,我不想保存原始xml,因为他们需要能够更新和存储它。我不确定您所说的什么应该成为studentrecord对象-请解释一下?谢谢,我知道你为什么想要一个数据库;但是为什么不能从XML中创建一个studentRecord对象,然后将其传递给数据库函数以获取studentRecord?因为我不知道如何实现:)谢谢你。有一次我喝了一些咖啡,我看了看代码,按照你的建议做了。问题解决了,我就是这么做的。再次感谢,我很感激
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();
    }
  }
}
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);
}