IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement Mobile en Java Discussion :

Ouvrir une Activité Calendrier à partir d'une Intent et du click sur un bouton


Sujet :

Développement Mobile en Java

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2020
    Messages : 6
    Points : 9
    Points
    9
    Par défaut Ouvrir une Activité Calendrier à partir d'une Intent et du click sur un bouton
    Bonjour à tous. Pour un projet perso en Android démarré pendant cette période de confinement, je cherche à ouvrir un Agenda depuis une Activité à partir sur un bouton.
    Dans la fonction qui pose le calendrier, je récupère également des évènements depuis une base de données SQLite et j'ouvre donc une base avec la fonction getReadableDatabase.

    Tout semble fonctionner quand j'ouvre l'activité Calendrier directement en en faisant l'activité principale depuis mon Manifest et dans ce cas la base de données ne renvoie pas d'erreur.

    Pourtant, quand je lance l'activité depuis une autre activité avec un Intent depuis mon écouteur de click, l'application crash et me renvoie comme message d'erreur que la base de données est "locked" :

    J'ai essayé différentes méthodes telles que

    Quand je lance l'activité

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
     
         newRdv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
     
                    Toast.makeText(PatientFolderActivity.this, "PETER DIAMANDIS & RAY KURZWEIL", Toast.LENGTH_LONG).show();
     
                    Intent intent = new Intent(PatientFolderActivity.this, CustomCalendarActivity.class);
     
                    startActivity(intent);
     
                }
            });
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
     
    public class CustomCalendarActivity extends AppCompatActivity {
     
        private static final String TAG = CustomCalendarActivity.class.getSimpleName();
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_custom_calendar);
            CalendarCustomView mView = findViewById(R.id.custom_calendar);
        }
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
     
     
     private void setUpCalendarAdapter() {
     
            String currwntDate = formatter.format(cal.getTime());
     
            currentDate.setText(currwntDate);
            dates.clear();
            //List<Date> dayValueInCells = new ArrayList<Date>();
            dbOpenHelper = new Database(context);
            //DatabaseQuery mQuery = new DatabaseQuery(context);
     
            //dbOpenHelper = Database.getInstance(context);
     
            List<EventObjects> mEvents = dbOpenHelper.getAllFutureEvents();
     
            //int mEvents = CalendarCustomView.this.openDatabase().getAllFutureEvents();
     
            Calendar mCal = (Calendar)cal.clone();
     
            mCal.set(Calendar.DAY_OF_MONTH, 1);
     
            int FirstDayOfTheMonth = mCal.get(Calendar.DAY_OF_WEEK) - 1;
     
            mCal.add(Calendar.DAY_OF_MONTH, -FirstDayOfTheMonth);
            //CollectEventsPerMonth(monthFormat.format(cal.getTime()), yearFormate.format(cal.getTime()));
     
            while(dates.size() < MAX_CALENDAR_COLUMN) {
     
                dates.add(mCal.getTime());
                mCal.add(Calendar.DAY_OF_MONTH, 1);
     
            }
     
            Log.d(TAG, "Number of date " + dates.size());
     
            //String sDate = formatter.format(cal.getTime());
            //currentDate.setText(sDate);
     
            mAdapter = new GridAdapter(context, dates, cal, mEvents);
            //mAdapter = new GridAdapter(context, dates, cal);
            calendarGridView.setAdapter(mAdapter);
     
        }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
     
     public List<EventObjects> getAllFutureEvents() {
            Date dateToday = new Date();
            List<EventObjects> events = new ArrayList<>();
            String query = "select * from " + TABLE_EVENTS + "";
            //String query1 = "SELECT * FROM " + TABLE_EVENTS;
            SQLiteDatabase db = getReadableDatabase();
            Cursor cursor = db.rawQuery(query, null);
     
            if(cursor.moveToFirst()){
                do{
                    int id = cursor.getInt(0);
                    String event = cursor.getString(cursor.getColumnIndexOrThrow("event"));
                    String date = cursor.getString(cursor.getColumnIndexOrThrow("date"));
                    String time = cursor.getString(cursor.getColumnIndexOrThrow("time"));
                    String month = cursor.getString(cursor.getColumnIndexOrThrow("month"));
                    String year = cursor.getString(cursor.getColumnIndexOrThrow("year"));
     
                    Date reminderDate = convertStringToDate(date);
     
                    if(reminderDate.after(dateToday) || reminderDate.equals(dateToday)) {
                        events.add(new EventObjects(id, event, date, time, month, year));
                    }
                }
                while (cursor.moveToNext());
            }
            cursor.close();
            db.close();
     
            return events;
     
        }
    Ayant trouvé sur Internet, que mon problème pourrait être en rapport avec le multithreading, j'ai tenté de transformer ma classe SQLiteOpenHelper en singleton :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
     
        private static Database sInstance;
     
     
        public static synchronized Database getInstance(Context context) {
     
            if (sInstance == null) {
                sInstance = new Database(context.getApplicationContext());
            }
     
            return sInstance;
        }
    Et d'ouvrir la connection ainsi depuis ma fonction setUpCalendarAdapter

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     
    dbOpenHelper = Database.getInstance(context);
    Pourtant, çà ne semble pas mieux fonctionner et je reçois encore cette erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
     
     
    E/SQLiteLog: (5) database is locked
    E/SQLiteDatabase: Failed to open database '/data/user/0/com.example.testcovid/databases/ wozniak '.
        android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
            at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:635)
            at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:321)
            at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:295)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:216)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:194)
            at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:493)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:200)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:192)
            at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:864)
            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:849)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:724)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:714)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:295)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:262)
            at com.example.testcovid.Database.getAllFutureEvents(Database.java:434)
            at com.example.testcovid.CalendarCustomView.setUpCalendarAdapter(CalendarCustomView.java:375)
            at com.example.testcovid.CalendarCustomView.<init>(CalendarCustomView.java:82)
            at java.lang.reflect.Constructor.newInstance0(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
            at android.view.LayoutInflater.createView(LayoutInflater.java:647)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
            at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
            at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
            at com.example.testcovid.CustomCalendarActivity.onCreate(CustomCalendarActivity.java:18)
            at android.app.Activity.performCreate(Activity.java:7009)
            at android.app.Activity.performCreate(Activity.java:7000)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
    E/SQLiteOpenHelper: Couldn't open  wozniak  for writing (will try read-only):
        android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
            at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:635)
            at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:321)
            at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:295)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:216)
            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:194)
            at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:493)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:200)
            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:192)
            at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:864)
            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:849)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:724)
            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:714)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:295)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:262)
            at com.example.testcovid.Database.getAllFutureEvents(Database.java:434)
            at com.example.testcovid.CalendarCustomView.setUpCalendarAdapter(CalendarCustomView.java:375)
            at com.example.testcovid.CalendarCustomView.<init>(CalendarCustomView.java:82)
            at java.lang.reflect.Constructor.newInstance0(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
            at android.view.LayoutInflater.createView(LayoutInflater.java:647)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
            at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
            at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
            at com.example.testcovid.CustomCalendarActivity.onCreate(CustomCalendarActivity.java:18)
            at android.app.Activity.performCreate(Activity.java:7009)
            at android.app.Activity.performCreate(Activity.java:7000)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
    D/AndroidRuntime: Shutting down VM
    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.testcovid, PID: 5405
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testcovid/com.example.testcovid.CustomCalendarActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.example.testcovid.CalendarCustomView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.example.testcovid.CalendarCustomView
         Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.testcovid.CalendarCustomView
         Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance0(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
            at android.view.LayoutInflater.createView(LayoutInflater.java:647)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
            at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
            at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
            at com.example.testcovid.CustomCalendarActivity.onCreate(CustomCalendarActivity.java:18)
            at android.app.Activity.performCreate(Activity.java:7009)
            at android.app.Activity.performCreate(Activity.java:7000)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
            at android.database.sqlite.SQLiteConnection.nativeExecuteForLong(Native Method)
            at android.database.sqlite.SQLiteConnection.executeForLong(SQLiteConnection.java:599)
            at android.database.sqlite.SQLiteSession.executeForLong(SQLiteSession.java:652)
            at android.database.sqlite.SQLiteStatement.simpleQueryForLong(SQLiteStatement.java:107)
            at android.database.DatabaseUtils.longForQuery(DatabaseUtils.java:842)
            at android.database.DatabaseUtils.longForQuery(DatabaseUtils.java:830)
            at android.database.sqlite.SQLiteDatabase.getVersion(SQLiteDatabase.java:940)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:311)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:262)
    E/AndroidRuntime:     at com.example.testcovid.Database.getAllFutureEvents(Database.java:434)
            at com.example.testcovid.CalendarCustomView.setUpCalendarAdapter(CalendarCustomView.java:375)
            at com.example.testcovid.CalendarCustomView.<init>(CalendarCustomView.java:82)
            	... 26 more

    Est-ce que quelqu'un aurait une idée de ce qui pourrait clocher ?

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2020
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2020
    Messages : 6
    Points : 9
    Points
    9
    Par défaut
    L'appel de la fonction de récupération des évènements depuis la base de données se fait depuis un objet de type LinearLayout.

    En analysant les messages d'erreurs reçus, je me suis dit que le problème venait peut-être du fait que la charge de travail était trop importante pour le Thread principal et j'ai cherché à récupérer les évènements depuis un Thread secondaire, afin de les renvoyer par la suite au Thread principal de cette façon(cf : la programmation en milieu multi-threadé est un nouveau concept pour moi).

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
     
     
      public List<EventObjects> getAllFutureEvents() {
            Date dateToday = new Date();
            List<EventObjects> events = new ArrayList<>();
     
            new Thread(new Runnable() {
     
                @Override
                public void run() {
     
                    try {
     
                        String query1 = "SELECT * FROM " + TABLE_EVENTS;
                        Cursor cursor = db.rawQuery(query1, null);
                        cursor.close();
                        db1.close();
     
                    }
                    catch(SQLiteDatabaseLockedException e) {
     
                        e.printStackTrace();
     
                    }
     
                    db1.close();
     
                }
            }).start();
     
            Looper.prepare();
     
            mHandler = new Handler() {
                public void handleMessage(Message msg) {
     
                }
            };
     
            Looper.loop();
     
            return events;
     
        }
    Pourtant, l'application crash toujours au moment de l'appel à la base de données, en me renvoyant notamment le message "database locked".

    Me disant que la charge de travail était trop importante, j'ai essayé d'exécuter l'appel à la base de données dans un Thread différent.


    Voici le code de mon fichier SQLiteOpenHelper :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
     
     
    package com.example.testcovid;
     
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabaseLockedException;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.os.Handler;
    import android.os.Looper;
    import android.os.Message;
    import android.util.Log;
    import android.widget.Toast;
    import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Locale;
    import java.util.concurrent.atomic.AtomicInteger;
     
    public class Database extends SQLiteOpenHelper {
     
        private static final String DATABASE_NAMES = " wozniak ";
        private static final int DATABASE_VERSION = 2;
        public static final String TABLE_EVENTS = " events ";
        public static final String TABLE_MEDECINS = " medecins ";
        public static final String TABLE_PATIENTS = " patients ";
        public static final String TABLE_QUIZ_PATIENTS = " quizs ";
        public static final String TABLE_RESEAU_MEDECINS = " reseau_medecins ";
        public static final String COLUMN_ID_EVENTS = " ID ";
        public static final String COLUMN_EVENT_EVENTS = " event ";
        public static final String COLUMN_DATE_EVENTS = " date ";
        public static final String COLUMN_TIME_EVENTS = " time ";
        public static final String COLUMN_MONTH_EVENTS = " month ";
        public static final String COLUMN_YEAR_EVENTS = " year ";
        public static final String COLUMN_ID_PATIENT_EVENTS = " id_patient ";
        public static final String COLUMN_ID_MEDECIN_EVENTS = " id_medecin ";
        public static final String COLUMN_ID_MEDECIN = " medecin_id ";
        public static final String COLUMN_NOM_MEDECIN = " medecin_nom ";
        public static final String COLUMN_PRENOM_MEDECIN = " medecin_prenom ";
        public static final String COLUMN_MEDECIN_RPPS = " medecin_rpps ";
        public static final String COLUMN_MEDECIN_MAIL = " medecin_mail ";
        public static final String COLUMN_MEDECIN_PORTABLE = " medecin_portable ";
        public static final String COLUMN_MEDECIN_VILLE = " medecin_ville ";
        public static final String COLUMN_MEDECIN_CODE_POSTAL = " medecin_code_postal ";
        public static final String COLUMN_MEDECIN_MOT_DE_PASSE = " medecin_mot_de_passe ";
        public static final String COLUMN_MEDECIN_CONFIR_MOT_DE_PASSE = " medecin_confir_mot_de_passe ";
        public static final String COLUMN_ID_PATIENT = " patient_id ";
        public static final String COLUMN_NOM_PATIENT = " patient_nom ";
        public static final String COLUMN_PRENOM_PATIENT = " patient_prenom ";
        public static final String COLUMN_MAIL_PATIENT = " patient_mail ";
        public static final String COLUMN_PORTABLE_PATIENT = " patient_portable ";
        public static final String COLUMN_VILLE_PATIENT = " patient_ville ";
        public static final String COLUMN_CODE_POSTAL_PATIENT = " patient_code_postal ";
        public static final String COLUMN_ID_QUIZ_PATIENT = " id_quiz_patient ";
        public static final String COLUMN_DATE_QUIZ_PATIENT = " date_quiz_patient ";
        public static final String COLUMN_RESULT_QUIZ_PATIENT = " result_quiz_patient ";
        public static final String COLUMN_RESEAU_MEDECINS_ID = " reseau_id ";
        public static final String COLUMN_MEDECIN_USER_ID = " medecin_user_id ";
        public static final String COLUMN_MEDECIN_ASSOCIATE = " medecin_associate_id ";
        private static Database sInstance;
        private AtomicInteger mOpenCounter = new AtomicInteger();
        public Handler mHandler;
        volatile SQLiteDatabase db1;
     
        private static final String CREATE_TABLE_EVENTS =
                "CREATE TABLE " + TABLE_EVENTS + "("
                        + COLUMN_ID_EVENTS + "INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
                        + COLUMN_EVENT_EVENTS + "TEXT NOT NULL, "
                        + COLUMN_DATE_EVENTS + "TEXT NOT NULL, "
                        + COLUMN_TIME_EVENTS + "TEXT NOT NULL, "
                        + COLUMN_MONTH_EVENTS + "TEXT NOT NULL, "
                        + COLUMN_YEAR_EVENTS + "TEXT NOT NULL " + ")";
     
        private static final String CREATE_TABLE_MEDECINS =
                "CREATE TABLE " + TABLE_MEDECINS + "("
                        + COLUMN_ID_MEDECIN + "INTEGER PRIMARY KEY AUTOINCREMENT, "
                        + COLUMN_NOM_MEDECIN + "TEXT, "
                        + COLUMN_PRENOM_MEDECIN + "TEXT, "
                        + COLUMN_MEDECIN_RPPS + "TEXT, "
                        + COLUMN_MEDECIN_MAIL + "TEXT, "
                        + COLUMN_MEDECIN_PORTABLE + "TEXT, "
                        + COLUMN_MEDECIN_VILLE + "TEXT, "
                        + COLUMN_MEDECIN_CODE_POSTAL + "TEXT, "
                        + COLUMN_MEDECIN_MOT_DE_PASSE + "TEXT, "
                        + COLUMN_MEDECIN_CONFIR_MOT_DE_PASSE + "TEXT " + ")";
     
        private static final String CREATE_TABLE_PATIENTS =
                "CREATE TABLE " + TABLE_PATIENTS + "("
                        + COLUMN_ID_PATIENT + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                        + COLUMN_NOM_PATIENT + " TEXT, "
                        + COLUMN_PRENOM_PATIENT + " TEXT, "
                        + COLUMN_MAIL_PATIENT + "TEXT, "
                        + COLUMN_PORTABLE_PATIENT + "TEXT, "
                        + COLUMN_VILLE_PATIENT + "TEXT, "
                        + COLUMN_CODE_POSTAL_PATIENT + "TEXT " + ")";
     
        private static final String DROP_TABLE_EVENTS = "DROP TABLE IF EXISTS " + DBStructure.EVENT_TABLE_NAME;
     
        public static synchronized Database getInstance(Context context) {
     
            if (sInstance == null) {
                sInstance = new Database(context.getApplicationContext());
            }
     
            return sInstance;
        }
     
        public Database(Context context) {
            super(context, DATABASE_NAMES, null, DATABASE_VERSION);
            db1 = this.getReadableDatabase();
        }
     
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE_EVENTS);
            db.execSQL(CREATE_TABLE_MEDECINS);
            db.execSQL(CREATE_TABLE_PATIENTS);
        }
     
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_EVENTS);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEDECINS);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_PATIENTS);
     
            onCreate(db);
        }
     
        public String insertMedecin(String nom, String prenom, String rpps, String mail, String portable, String ville, String code_postal, String mot_de_passe) {
     
            Log.d("nom", nom);
            Log.d("prenom", prenom);
            Log.d("rpps", rpps);
            Log.d("mail", mail);
            Log.d("portable", portable);
            Log.d("ville", ville);
            Log.d("code_postal", code_postal);
            Log.d("mot_de_passe", mot_de_passe);
     
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("medecin_nom", nom);
            contentValues.put("medecin_prenom", prenom);
            contentValues.put("medecin_rpps", rpps);
            contentValues.put("medecin_mail", mail);
            contentValues.put("medecin_portable", portable);
            contentValues.put("medecin_ville", ville);
            contentValues.put("medecin_code_postal", code_postal);
            contentValues.put("medecin_mot_de_passe", mot_de_passe);
     
            long result = db.insert(TABLE_MEDECINS, null, contentValues);
     
            Log.d("result", String.valueOf(result));
     
            if (result == -1) {
                return "false";
            } else {
                return "true";
            }
     
        }
     
        public Cursor getAllMedecins() {
     
            Log.d("TAG-SQLITE", "MERDE DE MERDE");
     
            SQLiteDatabase db = this.getReadableDatabase();
     
            Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_MEDECINS + ";", null);
     
            while(cursor.moveToNext()) {
     
                Log.d("TAG-SQLITE", cursor.getString(1));
                Log.d("TAG-SQLITE", cursor.getString(2));
                Log.d("TAG-SQLITE", cursor.getString(3));
                Log.d("TAG-SQLITE", cursor.getString(4));
                Log.d("TAG-SQLITE", cursor.getString(5));
                Log.d("TAG-SQLITE", cursor.getString(6));
                Log.d("TAG-SQLITE", cursor.getString(7));
     
            }
     
            Log.d("TAG-SQLITE", "MERDE DE MERDE");
     
            return cursor;
     
        }
     
        public ArrayList<Medecin> getAllPhysicians(Context context) {
     
            ArrayList<Medecin> list = new ArrayList<Medecin>();
     
            SQLiteDatabase db = this.getReadableDatabase();
            db.beginTransaction();
            String selectQuery = "SELECT * FROM " + TABLE_MEDECINS;
            Cursor cursor = db.rawQuery(selectQuery, null);
     
            if(cursor.getCount() > 0) {
     
                while(cursor.moveToNext()) {
     
                    Medecin m = new Medecin();
                    m.setIdMedecin(cursor.getInt(cursor.getColumnIndex("medecin_id")));
                    m.setNomMedecin(cursor.getString(cursor.getColumnIndex("medecin_nom")));
                    m.setPrenomMedecin(cursor.getString(cursor.getColumnIndex("medecin_prenom")));
                    m.setMailMedecin(cursor.getString(cursor.getColumnIndex("medecin_mail")));
                    m.setMailMedecin(cursor.getString(cursor.getColumnIndex("medecin_mot_de_passe")));
     
                    list.add(m);
     
                }
     
            }
     
            return list;
     
        }
     
        public void SaveEvent(String event, String date,  String time, String month, String year, SQLiteDatabase database) {
     
            ContentValues contentValues = new ContentValues();
            contentValues.put(DBStructure.EVENT, event);
            contentValues.put(DBStructure.DATE, date);
            contentValues.put(DBStructure.TIME, time);
            contentValues.put(DBStructure.MONTH, month);
            contentValues.put(DBStructure.YEAR, year);
     
            database.insert(DBStructure.EVENT_TABLE_NAME, null, contentValues);
     
            String query = "select * from events";
     
            SQLiteDatabase db = this.getReadableDatabase();
     
            Cursor cursor = db.rawQuery(query, null);
            if(cursor.moveToFirst()){
                do{
                    int id = cursor.getInt(0);
                    String eventE = cursor.getString(cursor.getColumnIndexOrThrow("event"));
                    String dateE = cursor.getString(cursor.getColumnIndexOrThrow("date"));
                    String timeE = cursor.getString(cursor.getColumnIndexOrThrow("time"));
                    String monthE = cursor.getString(cursor.getColumnIndexOrThrow("month"));
                    String yearE = cursor.getString(cursor.getColumnIndexOrThrow("year"));
                }
                while (cursor.moveToNext());
            }
            cursor.close();
     
        }
     
        public Cursor ReadEvents(String date, SQLiteDatabase database) {
     
            String[] Projections = {COLUMN_EVENT_EVENTS, COLUMN_TIME_EVENTS, COLUMN_DATE_EVENTS, COLUMN_MONTH_EVENTS, COLUMN_YEAR_EVENTS};
            String Selection = DBStructure.DATE + "=?";
     
            String[] SelectionArgs = {date};
     
            return database.query(DBStructure.EVENT_TABLE_NAME, Projections, Selection, SelectionArgs, null, null, null);
     
        }
     
        public Cursor ReadEventsPerMonth(String month, String year, SQLiteDatabase database) {
     
            String[] Projections = {COLUMN_EVENT_EVENTS, COLUMN_TIME_EVENTS, COLUMN_DATE_EVENTS, COLUMN_MONTH_EVENTS, COLUMN_YEAR_EVENTS};
            String Selection = COLUMN_MONTH_EVENTS + "=? and " + COLUMN_YEAR_EVENTS + "=?";
            String[] SelectionArgs = {month, year};
     
            return database.query(TABLE_EVENTS, Projections, Selection, SelectionArgs, null, null, null);
     
        }
     
        public void deleteEvent(String event, String date, String time, SQLiteDatabase database) {
     
            String selection = COLUMN_EVENT_EVENTS + "=? and " +  COLUMN_DATE_EVENTS + "=? and " + COLUMN_TIME_EVENTS + "=?";
            String[] selectionArg = {event, date, time };
            database.delete(TABLE_EVENTS, selection, selectionArg);
     
        }
     
        public void updateEvent(String date, String event, String time, SQLiteDatabase database) {
     
            ContentValues contentValues = new ContentValues();
            String selection =  DBStructure.DATE + "=? and " +  DBStructure.EVENT + "=? and " + DBStructure.TIME + "=?";
            String[] selectionArg = {date, event, time};
            database.update(TABLE_EVENTS,  contentValues, selection, selectionArg);
     
        }
     
        public List<EventObjects> getAllFutureEvents() {
            Date dateToday = new Date();
            List<EventObjects> events = new ArrayList<>();
            /*String query = "select * from " + TABLE_EVENTS + "";*/
     
            new Thread(new Runnable() {
     
                @Override
                public void run() {
     
                    try {
     
                        String query1 = "SELECT * FROM " + TABLE_EVENTS;
                        db1.beginTransaction();
                        db1.execSQL(query1);
                        //Cursor cursor = db.rawQuery(query1, null);
                        //cursor.close();
                        db1.close();
     
                    }
                    catch(SQLiteDatabaseLockedException e) {
     
                        e.printStackTrace();
     
                    }
     
                    db1.close();
     
                }
            }).start();
     
            Looper.prepare();
     
            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    Log.d("MESSAGE", "String.valueOf(events.get(0).getEvent())");
                }
            };
     
            Looper.loop();
     
     
     
            return events;
     
        }
     
        private Date convertStringToDate(String dateInString) {
     
            DateFormat format = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
            Date date = null;
     
            try {
                date = format.parse(dateInString);
            } catch (ParseException e) {
                e.printStackTrace();
            }
     
            return date;
        }
     
        public boolean insertPatient(String nom, String prenom, String mail, String portable, String ville, String code_postal) {
     
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("patient_nom", nom);
            contentValues.put("patient_prenom", prenom);
            contentValues.put("patient_mail", mail);
            contentValues.put("patient_portable", portable);
            contentValues.put("patient_ville", ville);
            contentValues.put("patient_code_postal", code_postal);
     
            long result = db.insert(TABLE_PATIENTS, null, contentValues);
     
            if(result == -1) {
                return false;
            }
            else {
                return true;
            }
     
        }
     
        public ArrayList<Patient> getAllPatients(Context context) {
     
            ArrayList<Patient> list = new ArrayList<Patient>();
     
            SQLiteDatabase db = this.getReadableDatabase();
            db.beginTransaction();
            String selectQuery = "SELECT * FROM " + TABLE_PATIENTS;
            Cursor cursor = db.rawQuery(selectQuery, null);
     
            if(cursor.getCount() > 0) {
     
                while(cursor.moveToNext()) {
     
                    Patient p = new Patient();
                    p.setIdPatient(cursor.getInt(cursor.getColumnIndex("patient_id")));
                    p.setNomPatient(cursor.getString(cursor.getColumnIndex("patient_nom")));
                    p.setPrenomPatient(cursor.getString(cursor.getColumnIndex("patient_prenom")));
                    p.setMailPatient(cursor.getString(cursor.getColumnIndex("patient_mail")));
                    p.setPhonePatient(cursor.getString(cursor.getColumnIndex("patient_portable")));
     
                    list.add(p);
     
                }
     
            }
     
            return list;
     
        }
     
        public boolean insertNewQuizPatient(String dateQuiz, Integer resultQuiz, Integer idPatient) {
     
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("quiz_dateQuiz", dateQuiz);
            contentValues.put("quiz_resultQuiz", resultQuiz);
            contentValues.put("quiz_idPatient", idPatient);
     
            long result = db.insert(TABLE_QUIZ_PATIENTS, null, contentValues);
     
            if(result == -1) {
                return false;
            }
            else {
                return true;
            }
     
        }
     
    }

Discussions similaires

  1. Ouvrir une page vide en cliquant sur un bouton
    Par CaNiBaLe dans le forum C#
    Réponses: 6
    Dernier message: 27/08/2015, 18h23
  2. [SP-2010] Infopath 2010 : ouvrir une url lors du clic sur un bouton
    Par kcizth dans le forum SharePoint
    Réponses: 1
    Dernier message: 27/06/2013, 13h20
  3. [SP-2010] Infopath 2010 : ouvrir une url lors du clic sur un bouton
    Par kcizth dans le forum SharePoint
    Réponses: 5
    Dernier message: 26/06/2013, 14h54
  4. Ouvrir une zone de texte avec un clic bouton
    Par DjBeGi dans le forum Access
    Réponses: 4
    Dernier message: 07/06/2006, 15h28
  5. Ouvrir une fenetre avec l'heure sur écran externe avec X11
    Par jamesleouf dans le forum Applications et environnements graphiques
    Réponses: 2
    Dernier message: 20/03/2006, 14h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo