This is button update in main
btnUpdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e(TAG,"ButtonUpdate clicked"); new UpdateDB(MainActivity.this); txtLastUpdateShow.setText(db.getLastUpdateTime()); } });
This is updatedb class:
public UpdateDB(Activity activity){
Log.e(TAG,"UpdateDB constructor");
this.mActivity = activity;
Log.e(TAG,"ButtonUpdate clicked");
// events = new ArrayList();
eventRetriever = new EventRetriever(mActivity);
eventRetriever.execute();
Log.e(TAG,"UpdateDB constructor async executed");
Log.e(TAG,"UpdateDB constructor async executed");
currentDate = Calendar.getInstance(); //Get the current date
db = new DBHandler(mActivity);
db.setLastUpdateTime();
db.deleteOldEvents();
Log.e(TAG,"DB updated");
Log.e(TAG,"DB updated");
Log.e(TAG,"DB deleteoldevents called,end of update db");
}
after asycntask executed here, it goes to website and download information. It takes up to 10 minutes sometimes.
Sometimes as soon as i hit button, the textview of lastupdated becomes today's date but sometimes it becomes at the end of asynctask.
Now methods in dbhelper class:
This is constructr:
public DBHandler(Context context) {//, Activity mActivity
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.e(TAG,"db handler construct");
}
}
public void deleteOldEvents(){
Log.e(TAG,"db handler deleteOldEvents");
Log.e( "Chico","db handler deleteOldEvents");
currentDate = Calendar.getInstance();
formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm");//for sqlite
String sql = "DELETE FROM "+TABLE_EVENTS+" WHERE "+KEY_END_TIME+" < ('"
+ formatter.format(currentDate.getTime())+"')"
+" OR "+KEY_END_TIME+" < ('"
+ formatter.format(currentDate.getTime()).substring(0,10)+"')";
db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(sql, null);
if (cursor.moveToFirst()) {
do {
Event event = new Event();
// will be deleted here
} while (cursor.moveToNext());
}
Log.e(TAG,"db handler delete events end");
}
public String getLastUpdateTime(){
Log.e(TAG,"db handler getLastUpdateTime()");
db = this.getWritableDatabase();
String query = "SELECT "+KEY_START_TIME+" from "+TABLE_EVENTS+" WHERE "+KEY_LINK+" = 'LastUpdate'";
Cursor c = db.rawQuery(query,null);
if (c != null && c.moveToFirst()) {
lastUpdateTime = c.getString(0);
Log.e("Chico","db handler getLastUpdateTime() lstupdate:+ "+lastUpdateTime+" "+c.getString(0));
}
return lastUpdateTime;
}
public void setLastUpdateTime() {
Log.e(TAG,"db handler setLastUpdateTime: ");
SQLiteDatabase db = this.getWritableDatabase();
formatter= new SimpleDateFormat("yyyy-MM-dd");
currentDate = Calendar.getInstance();
ContentValues values = new ContentValues();
values.put(KEY_START_TIME, formatter.format(currentDate.getTime())); // Event start
values.put(KEY_END_TIME, formatter.format(currentDate.getTime()));
db.insertWithOnConflict(TABLE_EVENTS,null,values,SQLiteDatabase.CONFLICT_REPLACE);
Log.e(TAG","db handler setLastUpdateTime: "+formatter.format(currentDate.getTime()));
db.update(TABLE_EVENTS, values, KEY_LINK +"="+ "'LastUpdate'", null);
db.close(); // Closing database connection
}
But log output says this after i hit update button:
ButtonUpdate clicked
UpdateDB constructor EventRetriever constructor onPreExecute() UpdateDB constructor async executed
doinbackground
db handler construct
db handler construct
db handler setLastUpdateTime:
doitbackground university itu
connect itu
db handler deleteOldEvents
db handler delete events sonu
DB updated
db handler getLastUpdateTime()
I stopped here the program.
This last log says, the buttınupdate at the beginning i wrote, it calls this to write to textview. But it cant find (before this it was also null.) Why cant it find? I set a lastupdate row in database in
setlastupdate()
Should i use a method to check if update successful? But this is not problem for now because even if it is not successful, it should write to main textview. Maybe i must use an intent to go back to main?
Only a dialog comes inprexecute and postexecute gone. SO, in still view of main.
As i told, sometimes it cant update the textview but sometimes not. I tried synchornized but it did not work.
And here, database only was managed by setlastupdate because, onpostexecute i write to databse and it was still in doitbackground. So no need synchronize?
Aucun commentaire:
Enregistrer un commentaire