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

Android Discussion :

Création de base de donnée et affichage [SQLite]


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 5
    Par défaut Création de base de donnée et affichage [SQLite]
    Bonsoir,

    Voila je suis developpeur sur un projet d'application android mais je nécessite une base de donnée pour pouvoir enregistrer des notifications dans la base de données et pouvoir les affichés et les supprimés.
    J'ai un code qui bloquer car je n'arrive pas a comprendre comment cela fonctionne pour l'affichage et l'ajout dans la base de donnée.
    Je ne sais pas comment procédé pour codé cela. J'ai une activité ou j'ai un listview qui devrait afficher les notrification et que je pourrais delete apres la lecture de celle-ci. J'ai une activité qui me permet d'ajouter a la base de données une notifications donc Un nom / Description / échéance mais celle-ci n'est pas relié a la base de donnée donc c'est juste un formulaire vide.

    Voici le code de l'application :

    Mon main :
    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
    package com.example.maxime.appli100;
     
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.text.format.DateFormat;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
     
    import java.util.Date;
    import java.util.List;
     
    public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
     
        public TextView jourt = null;
        public TextView moist = null;
        public TextView titre_agenda = null;
        public ImageView icon_agenda = null;
        public ImageView cercle_date_acceuil = null;
        public ImageButton slide_layout = null;
        public boolean verif = Boolean.parseBoolean(null);
     
        public static boolean RepNotif = false;
        private NotificationDataSource datasource;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
     
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();
     
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
     
            Date jour = new Date();
            CharSequence j = DateFormat.format("d", jour.getTime());
            jourt = (TextView) findViewById(R.id.jour);
            jourt.setText(j);
            Date mois = new Date();
            CharSequence m = DateFormat.format("MMM", mois.getTime());
            moist = (TextView) findViewById(R.id.mois);
            moist.setText(m);
     
            // Importation de la font de Assets
            Typeface typeface = Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf");
            // Assignation de la font au TextView
            moist.setTypeface(typeface);
            jourt.setTypeface(typeface);
     
            titre_agenda = (TextView) findViewById(R.id.titre_agenda);
            titre_agenda.setText("Cette semaine");
     
            icon_agenda = (ImageView) findViewById(R.id.icon_agenda);
            icon_agenda.setImageResource(R.drawable.vignette_agenda);
     
            cercle_date_acceuil = (ImageView) findViewById(R.id.cercle_date_acceuil);
            cercle_date_acceuil.setImageResource(R.drawable.cercle_date_acceuil);
     
            RelativeLayout slide_layout = (RelativeLayout) findViewById(R.id.notif1_layout);
            slide_layout.setOnClickListener(this);
     
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View Views) {
                    Intent addevent = new Intent(MainActivity.this, AddEvent.class);
                    startActivity(addevent);
                }
            });
     
            DataBase();
        }
     
        private void DataBase(){
            datasource = new NotificationDataSource(this);
            datasource.open();
     
            List<String> values = datasource.getAllName();
     
            // utilisez SimpleCursorAdapter pour afficher les
            // �l�ments dans une ListView
            ArrayAdapter<Notification> adapter = new ArrayAdapter<Notification>(this,
                    android.R.layout.simple_list_item_1, values);
            setListAdapter(adapter);
        }
     
        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }
     
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
     
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
     
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
     
            return super.onOptionsItemSelected(item);
        }
     
        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();
     
            if (id == R.id.nav_acceuil) {
                Intent intentacceuil = new Intent(this, MainActivity.class);
                intentacceuil.putExtra("Acceuil", "nav_acceuil");
                startActivity(intentacceuil);
            } else if (id == R.id.nav_planning_cours) {
                Intent intentplanning = new Intent(this, PlanningCours.class);
                intentplanning.putExtra("Planning", "nav_planning_cours");
                startActivity(intentplanning);
            } else if (id == R.id.nav_groupe_class) {
     
            } else if (id == R.id.nav_agenda) {
     
            } else if (id == R.id.nav_note) {
     
            } else if (id == R.id.nav_stats) {
     
            } else if (id == R.id.nav_profil) {
     
            }
     
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
     
        //Fonction click des notifs
        @Override
        public void onClick(View v) {
            notif(R.id.notif_acceuil);
     
        }
     
        // Fonction pour les notifs
        public void notif(int res){
            if (verif == false) {
                RelativeLayout layout_slide = (RelativeLayout) findViewById(res);
                layout_slide.setVisibility(View.VISIBLE);
                verif = true;
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout_slide.getLayoutParams();
                params.setMargins(0, 150, 0, 0);
                layout_slide.setLayoutParams(params);
                slide_layout = (ImageButton) findViewById(R.id.arrow_notif1);
                slide_layout.setImageResource(R.mipmap.ic_arrow_drop_up);
     
            } else if (verif == true) {
                RelativeLayout layout_slide = (RelativeLayout) findViewById(res);
                layout_slide.setVisibility(View.INVISIBLE);
                verif = false;
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout_slide.getLayoutParams();
                params.setMargins(0, -300, 0, 0);
                layout_slide.setLayoutParams(params);
                slide_layout = (ImageButton) findViewById(R.id.arrow_notif1);
                slide_layout.setImageResource(R.mipmap.ic_arrow_drop_down);
     
            }
        }
     
        // Sera appel�e par l'attribut onClick
        // des boutons d�clar�s dans main.xml
        public void onClick(View view) {
            @SuppressWarnings("unchecked")
            ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
            Comment comment = null;
            switch (view.getId()) {
                case R.id.add:
                    String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
                    int nextInt = new Random().nextInt(3);
                    // enregistrer le nouveau commentaire dans la base de donn�es
                    comment = datasource.createComment(comments[nextInt]);
                    adapter.add(comment);
                    break;
                case R.id.delete:
                    if (getListAdapter().getCount() > 0) {
                        comment = (Comment) getListAdapter().getItem(0);
                        datasource.deleteComment(comment);
                        adapter.remove(comment);
                    }
                    break;
            }
            adapter.notifyDataSetChanged();
        }
     
        @Override
        protected void onResume() {
            datasource.open();
            super.onResume();
        }
     
        @Override
        protected void onPause() {
            datasource.close();
            super.onPause();
        }
     
     
    }
    Base de donnée :
    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
    package com.example.maxime.appli100;
     
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.provider.BaseColumns;
     
    /**
     * Created by Maxime on 20/01/2016.
     */
    public final class DataBaseNotif extends SQLiteOpenHelper {
     
        /* Inner class that defines the table contents */
        public static abstract class DateBase implements BaseColumns {
            public static final String COLUMN_NAME_ID = "id";
            public static final String COLUMN_NAME_NAME = "nom";
            public static final String COLUMN_NAME_DESCRIPTION = "description";
            public static final String COLUMN_NAME_ECHEANCE = "echeance";
        }
     
     
        public static final int VERSION_TABLE = 1;
     
        public static final String NOTIF_TABLE_NAME = "Notification";
        public static final String NOTIF_TABLE_CREATE=
                "CREATE TABLE " + NOTIF_TABLE_NAME + " (" +
                        DateBase.COLUMN_NAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        DateBase.COLUMN_NAME_NAME + " TEXT, " +
                        DateBase.COLUMN_NAME_DESCRIPTION + " TEXT, " +
                        DateBase.COLUMN_NAME_ECHEANCE + " TEXT);";
     
        public static final String METIER_TABLE_DROP = "DROP TABLE IF EXISTS " + NOTIF_TABLE_NAME + ";";
     
        public DataBaseNotif(Context context) {
            super(context, NOTIF_TABLE_NAME, null, VERSION_TABLE);
        }
     
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(NOTIF_TABLE_CREATE);
        }
     
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL(METIER_TABLE_DROP);
            onCreate(db);
        }
     
        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            onUpgrade(db, oldVersion, newVersion);
        }
     
    }
    Les getter and setters de la base de donnée :
    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
    package com.example.maxime.appli100;
     
    /**
     * Created by Maxime on 20/01/2016.
     *
     */
    public class Notification {
        // Notez que l'identifiant est un long
        private String id;
        private String nom;
        private String description;
        private String echeance;
     
        public Notification(String id, String nom, String description, String echeance) {
            super();
            this.id = id;
            this.nom = nom;
            this.description = description;
            this.echeance = echeance;
        }
     
        public Notification(){
     
        }
     
        public String getId() {
            return id;
        }
     
        public void setId(String id) {
            this.id = id;
        }
     
        public String getNom() {
            return nom;
        }
     
        public String getDescription() {
            return description;
        }
     
        public String getEcheance() {
            return echeance;
        }
     
        public void setNom(String nom) {
            this.nom = nom;
        }
     
        public void setDescription(String description) {
            this.description = description;
        }
     
        public void setEcheance(String echeance) {
            this.echeance = echeance;
        }
     
        @Override
        public String toString() {
            return "Notification{" +
                    "id='" + id + '\'' +
                    ", nom='" + nom + '\'' +
                    ", description='" + description + '\'' +
                    ", echeance='" + echeance + '\'' +
                    '}';
        }
    }
    Les fonctions sur laquelle je bloque pour prendre les notifications et les affichés :
    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
    package com.example.maxime.appli100;
     
    /**
     * Created by Maxime on 20/01/2016.
     */
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
     
    import com.example.maxime.appli100.Notification;
     
    import java.util.ArrayList;
    import java.util.List;
     
    public class NotificationDataSource {
     
        // Champs de la base de données
        private SQLiteDatabase database;
        private DataBaseNotif dbHelper;
        private String[] allColumns = { DataBaseNotif.DateBase.COLUMN_NAME_ID,
                DataBaseNotif.DateBase.COLUMN_NAME_NAME,
                DataBaseNotif.DateBase.COLUMN_NAME_DESCRIPTION,
                DataBaseNotif.DateBase.COLUMN_NAME_ECHEANCE,};
     
        Notification notif = new Notification();
     
        public NotificationDataSource(Context context) {
            dbHelper = new DataBaseNotif(context);
        }
     
        public void open() throws SQLException {
            database = dbHelper.getWritableDatabase();
        }
     
        public void close() {
            dbHelper.close();
        }
     
        /*
            COMMANDE POUR GERER LE NOM DES NOTIFICATIONS
        */
     
        public String createName(String Name) {
            ContentValues values = new ContentValues();
            values.put(DataBaseNotif.DateBase.COLUMN_NAME_NAME, Name);
            long insertId = database.insert(DataBaseNotif.DateBase.COLUMN_NAME_NAME, null,
                    values);
            Cursor cursor = database.query(DataBaseNotif.DateBase.COLUMN_NAME_NAME,
                    allColumns, DataBaseNotif.DateBase.COLUMN_NAME_ID + " = " + insertId, null,
                    null, null, null);
            cursor.moveToFirst();
            String newName = cursorToName(cursor).getNom();
            cursor.close();
            return newName;
        }
     
        public void deleteName(Notification N) {
            String id = N.getId();
            System.out.println("Comment deleted with id: " + id);
            database.delete(DataBaseNotif.DateBase.COLUMN_NAME_NAME, DataBaseNotif.DateBase.COLUMN_NAME_ID
                    + " = " + id, null);
        }
     
     
        public List<String> getAllName() {
            List<String> names = new ArrayList<>();
     
            Cursor cursor = database.query(DataBaseNotif.DateBase.COLUMN_NAME_NAME,
                    allColumns, null, null, null, null, null);
     
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                String name = cursorToName(cursor).getNom(); //Ligne modifiée
                names.add(name);
                cursor.moveToNext();
            }
            // assurez-vous de la fermeture du curseur
            cursor.close();
            return names;
        }
     
     
        private Notification cursorToName(Cursor cursor) {
            Notification n = new Notification();
            n.setId(cursor.getString(0));
            n.setNom(cursor.getString(1));
            return n;
        }
     
        /*
            COMMANDE POUR GERER LA DESCRIPTION DES NOTIFICATIONS
        */
     
        public String createDescription(String Name) {
            ContentValues values = new ContentValues();
            values.put(DataBaseNotif.DateBase.COLUMN_NAME_DESCRIPTION, Name);
            long insertId = database.insert(DataBaseNotif.DateBase.COLUMN_NAME_DESCRIPTION, null,
                    values);
            Cursor cursor = database.query(DataBaseNotif.DateBase.COLUMN_NAME_DESCRIPTION,
                    allColumns, DataBaseNotif.DateBase.COLUMN_NAME_ID + " = " + insertId, null,
                    null, null, null);
            cursor.moveToFirst();
            String newName = cursorToName(cursor).getNom();
            cursor.close();
            return newName;
        }
     
        public void deleteDescription(Notification N) {
            String id = N.getId();
            System.out.println("Comment deleted with id: " + id);
            database.delete(DataBaseNotif.DateBase.COLUMN_NAME_DESCRIPTION, DataBaseNotif.DateBase.COLUMN_NAME_ID
                    + " = " + id, null);
        }
     
     
        public List<String> getAllDescription() {
            List<String> names = new ArrayList<>();
     
            Cursor cursor = database.query(DataBaseNotif.DateBase.COLUMN_NAME_DESCRIPTION,
                    allColumns, null, null, null, null, null);
     
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                String name = cursorToName(cursor).getNom(); //Ligne modifiée
                names.add(name);
                cursor.moveToNext();
            }
            // assurez-vous de la fermeture du curseur
            cursor.close();
            return names;
        }
     
     
        private Notification cursorToDescription(Cursor cursor) {
            Notification n = new Notification();
            n.setId(cursor.getString(0));
            n.setNom(cursor.getString(1));
            return n;
        }
     
        /*
            COMMANDE POUR GERER LES ECHEANCES DES NOTIFICATIONS
        */
     
        public String createEcheance(String Name) {
            ContentValues values = new ContentValues();
            values.put(DataBaseNotif.DateBase.COLUMN_NAME_ECHEANCE, Name);
            long insertId = database.insert(DataBaseNotif.DateBase.COLUMN_NAME_ECHEANCE, null,
                    values);
            Cursor cursor = database.query(DataBaseNotif.DateBase.COLUMN_NAME_ECHEANCE,
                    allColumns, DataBaseNotif.DateBase.COLUMN_NAME_ID + " = " + insertId, null,
                    null, null, null);
            cursor.moveToFirst();
            String newName = cursorToName(cursor).getNom();
            cursor.close();
            return newName;
        }
     
        public void deleteEcheance(Notification N) {
            String id = N.getId();
            System.out.println("Comment deleted with id: " + id);
            database.delete(DataBaseNotif.DateBase.COLUMN_NAME_ECHEANCE, DataBaseNotif.DateBase.COLUMN_NAME_ID
                    + " = " + id, null);
        }
     
     
        public List<String> getAllEcheance() {
            List<String> names = new ArrayList<>();
     
            Cursor cursor = database.query(DataBaseNotif.DateBase.COLUMN_NAME_ECHEANCE,
                    allColumns, null, null, null, null, null);
     
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                String name = cursorToName(cursor).getNom(); //Ligne modifiée
                names.add(name);
                cursor.moveToNext();
            }
            // assurez-vous de la fermeture du curseur
            cursor.close();
            return names;
        }
     
     
        private Notification cursorToEcheance(Cursor cursor) {
            Notification n = new Notification();
            n.setId(cursor.getString(0));
            n.setNom(cursor.getString(1));
            return n;
        }
    }
    Voici l'activité qui permet d'ajouté une notification (Non fonctionnel, il faut faire le lien avec la base de donnée):
    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
    package com.example.maxime.appli100;
     
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.os.Bundle;
    import android.os.Parcel;
    import android.provider.ContactsContract;
    import android.renderscript.ScriptGroup;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.widget.EditText;
    import android.widget.Toast;
    import com.example.maxime.appli100.MainActivity.*;
     
     
     
    public class AddEvent extends AppCompatActivity {
     
        private String titre;
        private String description;
        private String echeance;
     
        EditText titre_edit;
        EditText description_edit;
        EditText echeance_edit;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add_event);
            //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            //setSupportActionBar(toolbar);
     
            titre_edit = (EditText)findViewById(R.id.titre_event);
            description_edit = (EditText)findViewById(R.id.description_event);
            echeance_edit = (EditText)findViewById(R.id.echeance_event);
     
            android.support.v7.app.ActionBar actionBar = this.getSupportActionBar();
            actionBar.setDefaultDisplayHomeAsUpEnabled(true);
        }
     
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            MenuInflater inflater = getMenuInflater();
            //R.menu.menu est l'id de notre menu
            inflater.inflate(R.menu.add_event, menu);
            return true;
        }
     
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
     
            //noinspection SimplifiableIfStatement
            if (id == R.id.enregistrer) {
     
                titre = titre_edit.getText().toString();
                description = description_edit.getText().toString();
                echeance = echeance_edit.getText().toString();
     
                DataBaseNotif bdNotif = new DataBaseNotif(getApplicationContext());
                SQLiteDatabase db = bdNotif.getWritableDatabase();
     
                ContentValues values = new ContentValues();
                values.put(DataBaseNotif.DateBase.COLUMN_NAME_ID, id);
                values.put(DataBaseNotif.DateBase.COLUMN_NAME_NAME, titre);
                values.put(DataBaseNotif.DateBase.COLUMN_NAME_DESCRIPTION, description);
                values.put(DataBaseNotif.DateBase.COLUMN_NAME_ECHEANCE, echeance);
     
                db.insert(DataBaseNotif.NOTIF_TABLE_NAME, DataBaseNotif.DateBase.COLUMN_NAME_NAME, values);
                db.insert(DataBaseNotif.NOTIF_TABLE_NAME, DataBaseNotif.DateBase.COLUMN_NAME_DESCRIPTION, values);
                db.insert(DataBaseNotif.NOTIF_TABLE_NAME, DataBaseNotif.DateBase.COLUMN_NAME_ECHEANCE, values);
     
                DataBase();
     
                Toast.makeText(AddEvent.this, "OkaySuper", Toast.LENGTH_LONG).show();
                return true;
            }
     
            return super.onOptionsItemSelected(item);
        }
     
        public void DataBase(){
     
            DataBaseNotif bdNotif = new DataBaseNotif(getApplicationContext());
            SQLiteDatabase db = bdNotif.getReadableDatabase();
            Cursor c = db.rawQuery("select * from " + DataBaseNotif.NOTIF_TABLE_NAME, null);
     
     
            while (c.moveToNext()) {
                String id = c.getString(0);
                String nom = c.getString(1);
                String description = c.getString(2);
                String echeance = c.getString(3);
                Notification n = new Notification(id, nom, description, echeance);
     
            }
            c.close();
     
        }
    }
    Tout d'abord je te remercie si tu est arrivée jusque là.
    Si tout-fois tu arrive a m'aidez a comprendre comment fonctionne les base de donnée sous android et que tu est motivé a m'aidez n'hésite pas a me poser des questions si tu ne comprend pas certaine chose
    Merci pour ta futur réponse je l'espère.
    Bonne soirée .
    Rweisha

  2. #2
    Membre chevronné

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2014
    Messages
    262
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : Burkina Faso

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Finance

    Informations forums :
    Inscription : Août 2014
    Messages : 262
    Par défaut
    Citation Envoyé par Rweisha Voir le message
    Bonsoir,

    Voila je suis developpeur sur un projet d'application android mais je nécessite une base de donnée pour pouvoir enregistrer des notifications dans la base de données et pouvoir les affichés et les supprimés.
    J'ai un code qui bloquer car je n'arrive pas a comprendre comment cela fonctionne pour l'affichage et l'ajout dans la base de donnée.
    Je ne sais pas comment procédé pour codé cela. J'ai une activité ou j'ai un listview qui devrait afficher les notrification et que je pourrais delete apres la lecture de celle-ci. J'ai une activité qui me permet d'ajouter a la base de données une notifications donc Un nom / Description / échéance mais celle-ci n'est pas relié a la base de donnée donc c'est juste un formulaire vide.
    ...
    Rweisha
    Au juste qu'est-ce que tu n'arrive pas à faire dans ton appli ?

    Tu obtiens une erreur de compilation ou est-ce le résultat attendu qui n'est pas obtenu ?

  3. #3
    Membre chevronné

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2014
    Messages
    262
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : Burkina Faso

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Finance

    Informations forums :
    Inscription : Août 2014
    Messages : 262
    Par défaut
    Poster de cette manière ton code, ça n'encourage à lire.

    Surtout que les développeurs n'ont jamais le temps !

    Tu peux m'envoyer ton projet sur mon mail lankoande.yabo.dev@gmail.com.

  4. #4
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 5
    Par défaut
    OH MERCIII!
    Je perdais espoirs sur ce projet ^^
    Oui je sais que poster du code comme ça ce n'est pas la meilleur façon d'encourager les gens a m'aidez mais je ne savais pas comment procédé :/

    Dès que je peut je t'envoie le projet Android studio sur ta boite mail

    Bonne journée

Discussions similaires

  1. Réponses: 9
    Dernier message: 09/01/2012, 16h27
  2. Création de bases de données avec SQLite
    Par slix_alex dans le forum Android
    Réponses: 2
    Dernier message: 22/03/2011, 17h49
  3. programme Delphi permettant la création de base de donnée
    Par lassad dans le forum Bases de données
    Réponses: 2
    Dernier message: 18/10/2005, 14h06
  4. Réponses: 1
    Dernier message: 17/06/2004, 17h44
  5. Réponses: 3
    Dernier message: 24/10/2003, 21h46

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