jeudi 17 décembre 2015

Android sqlite auto check oncreate?

My code is that in main to execute new intent:

btnUni.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e(TAG,"ButtonUni clicked");
           // new RetrieveEvents("Uni");

            Intent intent = new Intent(MainActivity.this,ListEvents.class);
            intent.putExtra("Type","Uni");

            Log.e(TAG,"Intent created, now executing");
            startActivity(intent);
        }
    });

then listevents class oncreate:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = getBaseContext();
    db = new DBHandler(mContext);
  if(db==null)  Log.e(TAG, "null ");
    if(mContext==null)  Log.e(TAG, "mnull ");
    Log.e(TAG, "ListEvents activity oncreate");

BUt output is that in my log with my tag:

 ButtonUni clicked
 Intent created, now executing
 db handler construct
 ListEvents activity oncreate

I did not call or insert or another thing to database but it does this. Or am i seeing wrong?

If it is true, i created a new method to check if databse is active(checking the lastupdate day). So i can put inside that oncreate of dbhandler?

this is oncreate and consturcıotr of dbhandler

  public DBHandler(Context context) {//, Activity mActivity
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    Log.e(TAG,"db handler construct");
    //formatter = new SimpleDateFormat("yyyy-MM-DD HH:MM");

    currentDate = Calendar.getInstance(); //Get the current date

}


public void onCreate(SQLiteDatabase db) {
    Log.e(TAG,"db handleroncreta");
    String CREATE_EVENTS_TABLE = "CREATE TABLE If not exists " + TABLE_EVENTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_NAME + " TEXT not null,"
           + KEY_LOCATION + " TEXT not null," + KEY_TYPE + " TEXT not null,"
            + KEY_LINK + " TEXT not null,"
            + KEY_START_TIME + " TEXT not null,"
            + KEY_END_TIME + " TEXT not null,"//bu belki null olabilir
     + KEY_DESCRIPTION + " TEXT not null"//bu da null olabilir
    + ")";

    db.execSQL(CREATE_EVENTS_TABLE);
}

My update method if database is not updated is that inside update db class, this class is for to update db when button clicked:

  public UpdateDB(Activity activity){
        Log.e(TAG,"UpdateDB constructor");
        this.mActivity = activity;
       // events = new ArrayList();
        eventRetriever = new EventRetriever(mActivity);
        eventRetriever.execute();
        Log.e(TAG,"UpdateDB constructor async executed");
        currentDate = Calendar.getInstance(); //Get the current date
        lastUpdateTime=currentDate;
        Log.e(TAG,"DB updated");
    }


   public UpdateDB(Activity activity, Calendar curDate){
        Log.e(TAG,"UpdateDB constructor  check:");
        this.mActivity = activity;

        //databse update mi diye gunluk kontrol ediliyor.currentdate zaten var classta da farklı parametre olsun diye
        //mainden yollarken lazımdı.databasede type=lastUpdate olacak

        if(lastUpdateTime.get(Calendar.DAY_OF_MONTH)<curDate.get(Calendar.DAY_OF_MONTH)) {
            Log.e(TAG,"UpdateDB constructor  check, update yapiliyor");
            //events = new ArrayList();
            eventRetriever = new EventRetriever(mActivity);
            eventRetriever.execute();
            Log.e(TAG, "UpdateDB constructor  check async executed");
            lastUpdateTime=curDate;//
            Log.e(TAG,"DB updated");
        }//if 
    }//method 
}//class 

Basic constructor is first one. It is triggered when button clicked and then calls asynctask for network operating to retrieve.

Other constructor is for update database automatically everyday. Because i need to get everyday events.

I did not do but i will keep the lastupdate time in the database. And when updated, i will update the last update in database with "set lastupdate... where=".

If autocheck of dbhelper class is true, should i use inside of it?

Aucun commentaire:

Enregistrer un commentaire