Hank Wilson
Hank Wilson

Reputation: 519

Getting data from a HashMap into SQLite database

In my app I am retrieving records from a server. The records are in an xml file. I'm able to get the file down and parse it without any trouble. I'm storing the results in a HashMap and I'd like to be able to put those results into my app's SQLite database.

Here's the code for the 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);}         
        };

Now in my app, I have already created a data entry form that uses a class called StudentRecord, in my entry form I use this function to update the file

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

What's the best way to get my values from the HashMap to my NewRecord function? I've been looking at so long I think I've gone brain dead. Thank you

Upvotes: 0

Views: 2179

Answers (1)

JRaymond
JRaymond

Reputation: 11782

If i'm reading you right, it sounds like you need to move your addStudent function from the form/activity where it lives right now into some kind of "helper" class:

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

And then just make calls to that helper when you've parsed your 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);
}         

And then when you're done processing:

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

Upvotes: 1

Related Questions