Bonsoir,

Je suis sur un projet ou l'objectif est de crée une application. Je suis débutant sur ce langage donc j'ai été obliger de regardé sur le net des codes pour m'en inspirer. Le problème que je rencontre c'est que j'ai besoin de codé une Base de Donnée en SQLite directement dans l'application et a des moments j'ai besoin de prendre l'id d'une variable grâce a un getter mais ce getter est dans une autre classe.

Pour informations, j'ai une classe Notification qui contient tout les Getter et Setter pour la base de donnée. J'ai une classe DataBaseNotif qui crée la base de donnée. et ensuite la classe NotificationDataSource qui me permettra d'ajouter des informations dans ma BDD.

Voici le code de la base de donnée (DataBaseNotif):

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);
    }
 
}
Voici la classe Notification qui contient les setters et les getters pour rappel :
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 + '\'' +
                '}';
    }
}
Et donc pour finir voici la dernière class qui me pose un soucis. Les getters et setters sont inconnus de la classe alors qu'il sont crée dans la class Notification. Et je n'arrive pas a importé et utilisé ces Getters et Setters (Le problème est signaler par des commentaires /************/ en haut et en bas de l'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
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();
    }
 
    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);
        cursor.close();
        return newName;
    }
 
    public String getId(){
        return notif.getId();
    }
 
    public void deleteName(String 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);
            names.add(name);
            cursor.moveToNext();
        }
        // assurez-vous de la fermeture du curseur
        cursor.close();
        return names;
    }
 
    private String cursorToName(Cursor cursor) {
        String name = new String();
        /***************************************/
        /***************************************/
        /***************************************/
        name.setId(cursor.getLong(0));
        name.setName(cursor.getString(1));
        /***************************************/
        /***************************************/
        /***************************************/
        return name;
    }
}
En espérant que des personnes plus expérimente réussissent a résoudre les problèmes.
Si vous avez besoin de plus d'informations, n'hésité pas.
Cordialement.
Bonne soirée;
Rweisha.