dimanche 28 décembre 2014

Trouble writing and reading SQLite database in Android

I am writing an App that will read data from an xml file on a remote server and then place it into an SQLite database on the phone/tab. I know it is successfully reading the data from the xml and placing it into the SQLite .db file. However, the problem is the rest of my app acts as though the SQLite .db file is empty. It won't access the data until I exit the App and run it again. What am I doing wrong?


Here's how I get the data:



// Get Instrument data from XML
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG) {
// Get the name of the tag (eg data or datum)
String strName = instruments.getName();
if (strName.equals("datum")) {
bFoundInstruments = true;
String datumDate = instruments.getAttributeValue(null, "date");
Float datumWtioil = Float.parseFloat(instruments.getAttributeValue(null, "wtioil"));
Float datumHh = Float.parseFloat(instruments.getAttributeValue(null, "hh"));
Float datumPlat = Float.parseFloat(instruments.getAttributeValue(null, "plat"));


publishProgress(datumDate, Float.toString(datumWtioil), Float.toString(datumHh), Float.toString(datumPlat));

// add data to SQLite database
datasource.createInstrument(datumDate, Float.toString(datumWtioil), Float.toString(datumHh), Float.toString(datumPlat));


}
}


Now here's how I put it into the SQLite .db file using the createInstrument() method:



public Instrument createInstrument(String instrumentDate, String instrumentWtioil, String instrumentHh,
String instrumentPlat) {
ContentValues values = new ContentValues();
//Log.d("instrumentalityWtioil",instrumentWtioil);
values.put(ForecastSQLiteHelper.COLUMN_DATE, instrumentDate);
values.put(ForecastSQLiteHelper.COLUMN_DATA1, instrumentWtioil);
values.put(ForecastSQLiteHelper.COLUMN_DATA2, instrumentHh);
values.put(ForecastSQLiteHelper.COLUMN_DATA3, instrumentPlat);
long insertId = database.insert(ForecastSQLiteHelper.TABLE_DATA, null,
values);
Cursor cursor = database.query(ForecastSQLiteHelper.TABLE_DATA,
allColumns, ForecastSQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Instrument newInstrument = cursorToInstrument(cursor);
cursor.close();
return newInstrument;
}


Why isn't that data immediately available to the App? Why do I have to close the App and restart it before it's visible?


Any advice will be greatly appreciated.


Aucun commentaire:

Enregistrer un commentaire