-1

I created a database with a table named flagTable, this table only has two fields, which are id(auto increment) and an integer field. Next, in my program, I have a button that will trigger a thread to start. When the thread is starting, it constantly retrieve data from database, and check for the for the value, if the value is equal to one then it will trigger another new Thread, something like this:

 private class statusOfStrummingInAnotherDevice extends Thread {
    int value;
    public void run() {

        try{
            while(true){
                try{

                    if(flagCursor == null){
                        flagCursor = cdb1.getFlagAll();
                    }
                }catch(Exception e){break;}

                try{
                    Log.i("MAIN3ACTIVITY","getting status");
                    int size = cdb1.getSize(flagCursor);
                    Log.i("MAIN3ACTIVITY","SIZE is" + String.valueOf(xyz));
                    for(int i = 0 ; i < size ; i++){
                        flagCursor.moveToPosition(i);
                        Log.i("MAIN3ACTIVITY","getting status jkasdfasdf");
                        value = cdb1.getFlag();
                        if(value == 1){
                            Log.i("FLAGCURSOR=====>>>>","Succesful");
                            releasingNotes = new ReleasingNotes(IntendedChord);
                            releasingNotes.start();

                            //break;
                        }
                        cdb1.updateFlag(0);
                        Log.i("FLAGCURSOR=====>>>>",String.valueOf(value));
                    }
                    flagCursor = null;

                }catch(Exception e){break;}

                Log.i("MAIN3ACTIVITY","thread is sleeping");
                try {
                    Thread.sleep(1000);

                } catch (InterruptedException e) {
                    break;
                }

            }
        }catch(Exception e){

        }

    }
}

In the meantime, the data that were retrieved from the database is using this function:

public Cursor getFlagAll(){
    return getReadableDatabase().rawQuery(
            "SELECT _ID, flag from flagTable", null);
}

And, the data that were updated to the database through this method:

public int updateFlag(int i) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("flag",i);
        return db.update("flagTable" , contentValues , "_ID" + "= ?",new String[]{String.valueOf(1)});

    }

Now, above codes will give no error, however, the data that were retrieved from the database is always 1, it keeps trigger a new function. In my above codes, I stated if the value is equal to 1, then the current thread will trigger a new thread to start, When its finished, the program will update the current data to 0. So that, the next round of the infinite loop can stop triggering new thread until a the conditon is met. What is problem overhere? did my codes really updated the new value? or I need to referesh the database every time I updated a new value.

JackPowell
  • 137
  • 3
  • 14

1 Answers1

0

Use Listeners to your database.

use SQLiteTransactionListener and do your things in onCommit()

Some guide in details here : https://developer.android.com/reference/android/database/sqlite/SQLiteTransactionListener.html and

http://www.programcreek.com/java-api-examples/index.php?api=android.database.sqlite.SQLiteTransactionListener

erluxman
  • 18,155
  • 20
  • 92
  • 126
  • Hi, Thanks for the reply. Do you have a good reference or example that I can refer to? Fyi, I'm using MySQL instead Postggress – JackPowell Feb 01 '17 at 03:30
  • https://developer.android.com/reference/android/database/sqlite/SQLiteTransactionListener.html – erluxman Feb 01 '17 at 04:27
  • Hi, i started to google the API you mentioned above, however, i dont understand what is the API talking about, next, how do I do my things in onCommit() this function?Can you show me an example based on my question? thanks – JackPowell Feb 01 '17 at 05:35
  • http://www.programcreek.com/java-api-examples/index.php?api=android.database.sqlite.SQLiteTransactionListener this may help you – erluxman Feb 01 '17 at 05:45
  • Appreciate of the reference, however, the example its too complicated to understand. Would be good you can write an example based on my question by using SQLiteTransaction API. Thanks! – JackPowell Feb 01 '17 at 06:01
  • Please help me out, be struggling this for many days already..I dont know what is the real solution . – JackPowell Feb 01 '17 at 06:02
  • I haven't used it in person but I just know that implementing listener is possible, I have used it in realm though – erluxman Feb 01 '17 at 06:12
  • But I have to be sorry about the fact that i Cannot teach you how to implement listeners from stackoverflow ,, you have to go and search and find the resource digest them... there is no shortcut – erluxman Feb 01 '17 at 06:13
  • okay, do you know other alternatives to create a listener for database? thanks – JackPowell Feb 01 '17 at 06:14
  • The easiest way is to implement listeners – erluxman Feb 01 '17 at 06:19