Bonjour,
Je me tourne vers vous car j'ai quelques difficultés concernant la gestion de ma base SQLite sous Android.
J'ai une base qui ne contient qu'une table alerte, elle même composée de plusieurs champs que j'aimerais afficher dans un CustomListAdapter (CustomListAdapter que j'impémenterai au fur et à mesure de mon avancement).
Ce que je cherche à faire pour l'instant est afficher une partie du contenu de la table alerte :
le champ "objet", le champ "champ1", le champ "champ2", le champ "champ3" et le champ "niveau alerte" dans mon CustomListAdapter.

Je n'arrive pas à faire le lien entre les champs que je veux récupérer et le CustomListAdapter. J'arrive à l'afficher dynamiquement à partir d'une chaîne de caractères mais je ne vois pas comment faire pour l'afficher à partir d'une BDD.

Mon projet se compose de 5 classes :
  1. ma classe Alerte qui est composée du constructeur, des getteurs et setteurs :

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
 
package com.example.stransitic.tralerteaffdynbdd;
 
 
public class Alerte {
 
    private static int id;
    private static int nb_champs;
    private static String id_client;
    private static String moment_alerte;
    private static String moment_message;
    private static String niv_alerte;
    private static String obj_alerte;
    private static String champ1;
    private static String champ2;
    private static String champ3;
    private static String lien;
 
    public Alerte(){}
 
    public Alerte(int val_nb_champs, String val_id_client,String val_moment_alerte,
                  String val_moment_message, String val_niv_alerte, String val_obj_alerte,
                  String val_champ1, String val_champ2, String val_champ3, String val_lien){
 
        this.nb_champs=val_nb_champs;
        this.id_client=val_id_client;
        this.moment_alerte=val_moment_alerte;
        this.moment_message=val_moment_message;
        this.niv_alerte=val_niv_alerte;
        this.obj_alerte=val_obj_alerte;
        this.champ1=val_champ1;
        this.champ2=val_champ2;
        this.champ3=val_champ3;
        this.lien=val_lien;
    }
 
    public static int getId() {
        return id;
    }
 
    public static int getNbChamps() {
        return nb_champs;
    }
 
    public static String getIdClient(){return id_client;}
 
    public static String getMoment_alerte(){return moment_alerte;}
 
    public static String getMoment_message(){return moment_message;}
 
    public static String getNivAlerte(){return niv_alerte;}
 
    public static String getObjAlerte(){return obj_alerte;}
 
    public static String getChamp1() {
        return champ1;
    }
 
    public static String getChamp2() {
        return champ2;
    }
 
    public static String getChamp3() {
        return champ3;
    }
 
    public static String getLien(){return lien;}
 
 
    public void setId(int id) {
        this.id = id;
    }
 
    public void setNbChamps(int nb_champs) {
        this.nb_champs = nb_champs;
    }
 
    public void setIdClient(String id_client){this.id_client = id_client;}
 
    public void setMomentAlerte(String moment_alerte){this.moment_alerte=moment_alerte;}
 
    public void setMomentMessage(String moment_message){this.moment_message=moment_message;}
 
    public void setNivAlerte(String niv_alerte) {
        this.niv_alerte = niv_alerte;
    }
 
    public void setObjAlerte(String obj_alerte){this.obj_alerte = obj_alerte;}
 
    public void setChamp1(String champ1) {
        this.champ1 = champ1;
    }
 
    public void setChamp2(String champ2) {
        this.champ2 = champ2;
    }
 
    public void setChamp3(String champ3) {
        this.champ3 = champ3;
    }
 
    public void setLien(String lien){
        this.lien=lien;
    }
 
    // Sera utilis�e par ArrayAdapter dans la ListView
    @Override
    public String toString() {
        return "numero d'alerte : "+id + "\nnombre de champs: " + nb_champs
                + "\nid client: "+ id_client + "\nheure de l'alerte: " + moment_alerte
                + "\nheure du message: "+ moment_message+ "\nniveau alerte: " + niv_alerte
                +"\nobjet de l'alerte: " + obj_alerte + "\nchamp 1: " + champ1
                + "\nchamp 2: " + champ2 + "\nchamp 3: " + champ3 + "\nlien: " + lien;
    }
}
  • ma classe MaBaseSQlite qui est chargée de définir la table lors de l'instanciation :


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
package com.example.stransitic.tralerteaffdynbdd;
 
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
 
public class MaBaseSQLite extends SQLiteOpenHelper {
 
    public static final String TABLE_ALERTE = "table_alerte";
    public static final String COL_ID = "id";
    public static final String COL_NB_CHAMPS = "nb_champs";
    public static final String COL_ID_CLIENT = "id_client";
    public static final String COL_MOMENT_ALERTE = "moment_alerte";
    public static final String COL_MOMENT_MESSAGE = "moment_message";
    public static final String COL_NIV_ALERTE = "niv_alerte";
    public static final String COL_OBJ_ALERTE = "obj_alerte";
    public static final String COL_CHAMP1 = "champ1";
    public static final String COL_CHAMP2 = "champ2";
    public static final String COL_CHAMP3 = "champ3";
    public static final String COL_LIEN = "lien";
 
    private static final String DATABASE_NAME = "alerte.db";
    private static final int DATABASE_VERSION = 1;
 
    // Commande sql pour la cr�ation de la base de donn�es
    private static final String CREATE_DATABASE = " CREATE TABLE " + TABLE_ALERTE + "( " + COL_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, "+COL_NB_CHAMPS+ " BIGINT, "
            + COL_ID_CLIENT+ " TEXT, " +COL_MOMENT_ALERTE+ " DATETIME, " + COL_MOMENT_MESSAGE + " DATETIME, "
            + COL_NIV_ALERTE+" INTEGER, "+COL_OBJ_ALERTE+ " TEXT, " + COL_CHAMP1 + " TEXT, " + COL_CHAMP2
            +" TEXT, " + COL_CHAMP3+" TEXT , " + COL_LIEN + " TEXT);";
 
    public MaBaseSQLite(Context context, String name, SQLiteDatabase.CursorFactory factory,
                        int version){
        super (context, name, factory, version);
    }
 
    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(CREATE_DATABASE);
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        //pour l'instant il on supprime juste la vieille base pour enregistrer la nouvelle.
        //a modifier apr�s pour ajouter � la suite
        db.execSQL("DROP TABLE "+TABLE_ALERTE+";" );
        onCreate(db);
    }
}
  • ma class AlerteBDD qui permet l'ajout, la suppression, la modification et requêtes pour récupérer le contenu de la BDD :


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
package com.example.stransitic.tralerteaffdynbdd;
 
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.internal.app.ToolbarActionBar;
 
 
 
public class AlerteBDD {
    private static final String DATABASE_NAME = "ma_base_alerte.db";
    private static final int DATABASE_VERSION = 1;
 
    private static final String TABLE_ALERTE = "table_alerte";
    private static final String COL_ID = "id";
    private static final int NUM_COL_ID = 0;
    private static final String COL_NB_CHAMPS = "nb_champs";
    private static final int NUM_COL_NB_CHAMPS =1 ;
    private static final String COL_ID_CLIENT = "id_client";
    private static final int NUM_COL_ID_CLIENT = 2;
    private static final String COL_MOMENT_ALERTE = "moment_alerte";
    private static final int NUM_COL_MOMENT_ALERTE = 3;
    private static final String COL_MOMENT_MESSAGE = "moment_message";
    private static final int NUM_COL_MOMENT_MESSAGE = 4;
    private static final String COL_NIV_ALERTE = "niv_alerte";
    private static final int NUM_COL_NIV_ALERTE = 5;
    private static final String COL_OBJ_ALERTE = "obj_alerte";
    private static final int NUM_COL_OBJ_ALERTE = 6;
    private static final String COL_CHAMP1 = "champ1";
    private static final int NUM_COL_CHAMP1 = 7;
    private static final String COL_CHAMP2 = "champ2";
    private static final int NUM_COL_CHAMP2 = 8;
    private static final String COL_CHAMP3 = "champ3";
    private static final int NUM_COL_CHAMP3 = 9;
    private static final String COL_LIEN = "lien";
    private static final int NUM_COL_LIEN = 10;
 
    private SQLiteDatabase bdd;
 
    private MaBaseSQLite maBaseSQLite;
 
    public AlerteBDD(Context context){
        //creation bdd et table
        maBaseSQLite = new MaBaseSQLite(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    public void open(){
        //on ouvre la BDD en �criture
        bdd = maBaseSQLite.getWritableDatabase();
    }
 
    public void close(){
        //fermeture bdd
        bdd.close();
    }
 
    public SQLiteDatabase getBDD(){
        return bdd;
    }
 
    public long insertAlerte(Alerte alerte){
        //creation d'un ContentValues (fonctionne comme HashMap)
        ContentValues values = new ContentValues();
        //values.put(COL_ID,Alerte.getId());
        values.put(COL_NB_CHAMPS,Alerte.getNbChamps());
        values.put(COL_ID_CLIENT,Alerte.getIdClient());
        values.put(COL_MOMENT_ALERTE, Alerte.getMoment_alerte());
        values.put(COL_MOMENT_MESSAGE, Alerte.getMoment_message());
        values.put(COL_NIV_ALERTE,Alerte.getNivAlerte());
        values.put(COL_OBJ_ALERTE,Alerte.getObjAlerte());
        values.put(COL_CHAMP1, Alerte.getChamp1());
        values.put(COL_CHAMP2, Alerte.getChamp2());
        values.put(COL_CHAMP3, Alerte.getChamp3());
        values.put(COL_LIEN, Alerte.getLien());
        return bdd.insert(TABLE_ALERTE, null, values);
    }
 
    public int updateAlerte(int id, Alerte alerte){
        //update BDD avec ID
 
        ContentValues values = new ContentValues();
        //values.put(COL_ID,Alerte.getId());
        values.put(COL_NB_CHAMPS,Alerte.getNbChamps());
        values.put(COL_ID_CLIENT,Alerte.getIdClient());
        values.put(COL_MOMENT_ALERTE, Alerte.getMoment_alerte());
        values.put(COL_MOMENT_MESSAGE, Alerte.getMoment_message());
        values.put(COL_NIV_ALERTE,Alerte.getNivAlerte());
        values.put(COL_OBJ_ALERTE,Alerte.getObjAlerte());
        values.put(COL_CHAMP1, Alerte.getChamp1());
        values.put(COL_CHAMP2, Alerte.getChamp2());
        values.put(COL_CHAMP3, Alerte.getChamp3());
        values.put(COL_LIEN, Alerte.getLien());
        return  bdd.update(TABLE_ALERTE, values, COL_ID + " = " + id, null);
    }
 
    public int removeAlerteWithId(int id){
        //suppression BDD avec ID
        return  bdd.delete(TABLE_ALERTE, COL_ID + " = " + id, null);
    }
 
 
    public Alerte getAlerteWithChamp1(String chmp1){
        Cursor c = bdd.query(TABLE_ALERTE, new String[]{COL_ID, COL_NB_CHAMPS, COL_ID_CLIENT,
                COL_MOMENT_ALERTE, COL_MOMENT_MESSAGE, COL_NIV_ALERTE, COL_OBJ_ALERTE, COL_CHAMP1,
                COL_CHAMP2, COL_CHAMP3, COL_LIEN}, COL_CHAMP1 + " LIKE \""
                + chmp1 + "\"", null, null, null, null);
        return cursorToAlerte(c);
    }
 
    public Alerte getAlerteWithObjet(String obj){
        Cursor c = bdd.query(TABLE_ALERTE, new String[]{COL_ID, COL_NB_CHAMPS, COL_ID_CLIENT,
                COL_MOMENT_ALERTE, COL_MOMENT_MESSAGE, COL_NIV_ALERTE, COL_OBJ_ALERTE, COL_CHAMP1,
                COL_CHAMP2, COL_CHAMP3, COL_LIEN}, COL_OBJ_ALERTE + " LIKE \""
                + obj + "\"", null, null, null, null);
        return cursorToAlerte(c);
    }
 
    public Alerte getAlerteWithId(int id){
        Cursor c = bdd.query(TABLE_ALERTE, new String[]{COL_ID, COL_NB_CHAMPS, COL_ID_CLIENT,
                COL_MOMENT_ALERTE, COL_MOMENT_MESSAGE, COL_NIV_ALERTE, COL_OBJ_ALERTE, COL_CHAMP1,
                COL_CHAMP2, COL_CHAMP3, COL_LIEN}, COL_ID + " LIKE \""
                + id + "\"", null, null, null, null);
        return cursorToAlerte(c);
    }
 
 
 
    public Alerte getAlerteWithNivAlerte(int niv){
        Cursor c = bdd.query(TABLE_ALERTE, new String[]{COL_ID, COL_NB_CHAMPS, COL_ID_CLIENT,
                COL_MOMENT_ALERTE, COL_MOMENT_MESSAGE, COL_NIV_ALERTE, COL_OBJ_ALERTE, COL_CHAMP1,
                COL_CHAMP2, COL_CHAMP3, COL_LIEN}, COL_NIV_ALERTE + " LIKE \""
                + niv + "\"", null, null, null, null);
        return cursorToAlerte(c);
    }
 
 
 
 
    private Alerte cursorToAlerte(Cursor c){
        //si aucun element retourn� dans requ�te, on renvoie null
        if(c.getCount()==0)
            return  null;
 
        //sinon on se deplace sur le premier element
        c.moveToFirst();
        //creation d'une Alerte
        Alerte alerte = new Alerte();
        alerte.setId(c.getInt(NUM_COL_ID));
        alerte.setNbChamps(c.getInt(NUM_COL_NB_CHAMPS));
        alerte.setIdClient(c.getString(NUM_COL_ID_CLIENT));
        alerte.setMomentAlerte(c.getString(NUM_COL_MOMENT_ALERTE));
        alerte.setMomentMessage(c.getString(NUM_COL_MOMENT_MESSAGE));
        alerte.setNivAlerte(c.getString(NUM_COL_NIV_ALERTE));
        alerte.setObjAlerte(c.getString(NUM_COL_OBJ_ALERTE));
        alerte.setChamp1(c.getString(NUM_COL_CHAMP1));
        alerte.setChamp2(c.getString(NUM_COL_CHAMP2));
        alerte.setChamp3(c.getString(NUM_COL_CHAMP3));
        alerte.setLien(c.getString(NUM_COL_LIEN));
        return alerte;
    }
 
    /*public AlerteBDD tableSize (){
        Cursor c = bdd.rawQuery(" SELECT COUNT (*) FROM" + TABLE_ALERTE);
        return SELECT (*) FROM TABLE_ALERTE;
    }*/
 
}
  • j'ai ma classe CorpsApp qui se charge d'insérer des données d'une chaîne de caractères dans la base. Et qui en théorie doit aussi injecter le contenu des champs voulus dans le CustomListAdapter :


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
 
///test splits foncionnel
public class CorpsApp extends Activity {
 
    ListView lv;
    final String FichierNick = "nick.txt";
    final String repertoire="infosbase";
 
 
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_corps_app);
 
        MainActivity.getInstance().finish();
 
        injectionDansBase();
 
        final TextView text =(TextView)findViewById(R.id.nick_tech);
        text.setText("Identifiant : " + InfosInit.lireNick(repertoire, FichierNick));
 
        lv= (ListView)findViewById(R.id.listView1);
 
        //ArrayList<CustomListAdapter.Alerte> alerte_details = getListData();
        //lv.setAdapter(new CustomListAdapter(this, alerte_details));
 
        Button btnArrierePlan=(Button)findViewById(R.id.btn_arriere_plan);
        btnArrierePlan.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                moveTaskToBack(true);
            }
        });
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }
 
    @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.
        switch (item.getItemId()) {
 
            case R.id.action_changer_ip:
                Intent secondeActivite = new Intent(CorpsApp.this, ClassIP.class);
                startActivity(secondeActivite);
                return true;
            case R.id.action_changer_nick:
                Intent troisiemeActivite = new Intent(CorpsApp.this, ClassNick.class);
                startActivity(troisiemeActivite);
                return true;
            case R.id.action_reinitialisation:
                Intent quatriemeActivite = new Intent(CorpsApp.this, ClassIPInit.class);
                startActivity(quatriemeActivite);
                return true;
            case R.id.action_quitter:
                Toast.makeText(this, "On quitte l'appli", Toast.LENGTH_LONG).show();
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
 
    /*private ArrayList<CustomListAdapter.Alerte> getListData(){
        ArrayList<CustomListAdapter.Alerte> resultats = new ArrayList<>();
 
        String str = "<stx>5;Objet1;bla1.1;bla1.2;bla1.3<etx><stx>3;Objet2;bla2.1;bla2.2;bla2.3<etx><stx>1;Objet3;bla3.1;bla3.2;bla3.3<etx><stx>7;Objet1;bla1.1;bla1.2;bla1.3<etx><stx>5;Objet2;bla2.1;bla2.2;bla2.3<etx><stx>1;Objet3;bla3.1;bla3.2;bla3.3<etx><stx>7;Objet1;bla1.1;bla1.2;bla1.3<etx><stx>5;Objet2;bla2.1;bla2.2;bla2.3<etx><stx>1;Objet3;bla3.1;bla3.2;bla3.3<etx>";
 
        String[] tMessage;
        String[] tChamp ;
 
        String delimiter1 =";";
        String delimiter = "<etx><stx>";
 
        tMessage = str.split(delimiter);
        int tailleTMessage = tMessage.length;
 
        String[][] tFinal = new String[tailleTMessage][5];
 
        CustomListAdapter.Alerte newsData = new CustomListAdapter.Alerte();
 
        //insert les donn�es des 2 splits cons�cutifs dans tFinal
        for(int i =0;i<tMessage.length;i++){
 
            tChamp = tMessage[i].split(delimiter1);
            newsData=new CustomListAdapter.Alerte();
 
            for(int j = 0;j<tChamp.length;j++){
 
                tFinal[i][j]=tChamp[j];
 
                switch(j){
                    case 0:
                        newsData.setObjet(tFinal[i][j]);
                        break;
                    case 1:
                        newsData.setChamp1(tFinal[i][j]);
                        break;
                    case 2:
                        newsData.setChamp2(tFinal[i][j]);
                        break;
                    case 3:
                        newsData.setChamp3(tFinal[i][j]);
                        break;
                    default :
                        Toast.makeText(getApplicationContext(), "ca ne marche pas ", Toast.LENGTH_LONG).show();
                }
            }
            resultats.add(newsData);
        }
        return resultats;
    }*/
 
 
    private void injectionDansBase()   {
 
        //creation instance de classe de BDD
        AlerteBDD alerteBDD = new AlerteBDD(this);
        alerteBDD.open();
 
        String str = "<stx>5;Objet1.1;bla1.1;bla1.2;bla1.3<etx><stx>3;Objet2.1;bla2.1;bla2.2;bla2.3<etx><stx>1;Objet3.1;bla3.1;bla3.2;bla3.3<etx><stx>7;Objet1.2;bla1.1;bla1.2;bla1.3<etx><stx>5;Objet2.2;bla2.1;bla2.2;bla2.3<etx><stx>1;Objet3.2;bla3.1;bla3.2;bla3.3<etx><stx>7;Objet1.3;bla1.1;bla1.2;bla1.3<etx><stx>5;Objet2.3;bla2.1;bla2.2;bla2.3<etx><stx>1;Objet3.3;bla3.1;bla3.2;bla3.3<etx>";
 
        String[] tMessage;
        String[] tChamp ;
 
        String delimiter = "<etx><stx>";
        String delimiter1 =";";
 
        tMessage = str.split(delimiter);
 
        String[][] tFinal = new String[tMessage.length][6];
 
        //insert les donn�es des 2 splits cons�cutifs dans tFinal
        for(int i =0;i<tMessage.length;i++){
 
            tChamp = tMessage[i].split(delimiter1);
 
            //creation alerte
            Alerte alerte = new Alerte();
 
            for(int j = 0;j<tChamp.length;j++){
 
 
                //remplacement chaine de debut
                tChamp[0]=tChamp[0].replace("<stx>","");
 
                tFinal[i][j]=tChamp[j];
 
                switch(j){
                    case 0:
                        alerte.setNivAlerte(tFinal[i][j]);
                        break;
                    case 1:
                        alerte.setObjAlerte(tFinal[i][j]);
                        break;
                    case 2:
                        alerte.setChamp1(tFinal[i][j]);
                        break;
                    case 3:
                        alerte.setChamp2(tFinal[i][j]);
                        break;
                    case 4:
                        alerte.setChamp3(tFinal[i][j]);
                        break;
                    default :
                        Toast.makeText(getApplicationContext(), "ca ne marche pas ! ", Toast.LENGTH_LONG).show();
                }
 
                tChamp[tChamp.length-1]=tChamp[tChamp.length - 1].replace("<etx>","");
 
 
            }
            alerteBDD.insertAlerte(alerte);
 
            Alerte alerteFromBDD = alerteBDD.getAlerteWithObjet(alerte.getObjAlerte());
 
            //recup date et heure courante
            Date CurDate = new Date();
            SimpleDateFormat d = new SimpleDateFormat("HHmmss");
            String s = d.format(CurDate);
            //injection date et heure dans BDD
            alerteFromBDD.setMomentMessage(s);
 
            if (alerteFromBDD==null)
                Toast.makeText(this, "Cette alerte n'existe pas dans la BDD", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(this, alerteFromBDD.toString(), Toast.LENGTH_LONG).show();
        }
        alerteBDD.close();
    }
}
  • enfin j'ai ma classe chargée de mon affichage dans la liste :


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
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
 
import java.util.ArrayList;
 
 
/**
 * Created by STRANSITIC on 22/07/2015.
 */
public class CustomListAdapter extends BaseAdapter {
 
    ArrayList<Alerte> listAlerte;
    LayoutInflater layoutInflater;
 
    public CustomListAdapter(Context context,ArrayList<Alerte> listAlerte) {
        this.listAlerte = listAlerte;
        layoutInflater = LayoutInflater.from(context);
    }
 
    public int getCount() {
// TODO Auto-generated method stub
        return listAlerte.size();
    }
 
    public Object getItem(int arg0) {
// TODO Auto-generated method stub
        return listAlerte.get(arg0);
    }
 
    public long getItemId(int arg0) {
// TODO Auto-generated method stub
        return arg0;
    }
 
    public View getView(int arg0, View arg1, ViewGroup arg2) {
 
        ViewHolder holder;
 
        if (arg1==null)
        {
            arg1= layoutInflater.inflate(R.layout.row_list, null);
            holder = new ViewHolder();
            // Objet correspondant � la textView associ� a l'objet
            holder.Objet = (TextView) arg1.findViewById(R.id.objet);
             // champ1 correspondant � la textView associ� au champ1
            holder.Champ1 = (TextView) arg1.findViewById(R.id.champ1);
              // champ2 correspondant � la textView associ� au champ2
            holder.Champ2 = (TextView) arg1.findViewById(R.id.champ2);
            // champ3 correspondant � la textView associ� au champ3
            holder.Champ3 = (TextView) arg1.findViewById(R.id.champ3);
            // Moment alerte
            holder.MomentAlerte = (TextView) arg1.findViewById(R.id.temps_passe);
            //niveau alerte
            holder.NiveauAlerte = (TextView) arg1.findViewById(R.id.niv_alerte);
            arg1.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) arg1.getTag();
        }
// mettre les donn�es dans chaque composante associ�e
// listEcoles.get(arg0).getNom() : extraire le nom de la listEcoles etc.
        holder.Objet.setText(listAlerte.get(arg0).getObjet());
        holder.Champ1.setText(listAlerte.get(arg0).getChamp1());
        holder.Champ2.setText(listAlerte.get(arg0).getChamp2());
        holder.Champ3.setText(listAlerte.get(arg0).getChamp3());
        holder.MomentAlerte.setText(listAlerte.get(arg0).getMomentAlerte());
        holder.NiveauAlerte.setText(listAlerte.get(arg0).getNiveauAlerte());
        return arg1;
    }
 
    static class ViewHolder {
        TextView Objet;
        TextView Champ1;
        TextView Champ2;
        TextView Champ3;
        TextView MomentAlerte;
        TextView NiveauAlerte;
    }
 
    public final static class Alerte  {
        String objet;
        String champ1;
        String champ2;
        String champ3;
        String momentAlerte;
        String niveauAlerte;
 
        public String getObjet(){
            return objet;
        }
 
        public void setObjet(String objet) {
            this.objet = objet;
        }
 
        public String getChamp1(){
            return champ1;
        }
 
        public void setChamp1(String champ1) {
            this.champ1 = champ1;
        }
 
        public String getChamp2(){
            return champ2;
        }
 
        public void setChamp2(String champ2) {
            this.champ2 = champ2;
        }
 
        public String getChamp3(){
            return champ3;
        }
 
        public void setChamp3(String champ3) {
            this.champ3 = champ3;
        }
 
        public String getMomentAlerte(){return momentAlerte;}
 
        public void setMomentAlerte(String momAlerte){this.momentAlerte= momAlerte;}
 
        public String getNiveauAlerte() {return  niveauAlerte;};
 
        public void setNiveauAlerte(String nivAlerte){this.niveauAlerte = nivAlerte;}
    }
}
J'ai par ailleurs mon fichier xml chargé de la vue :

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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <LinearLayout android:id="@+id/LinearLayout02"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/couleur_alerte"
            android:layout_height="fill_parent"
            android:layout_width="15dip"
            android:layout_column="3"
            android:background="#DB0B32"/>
        <RelativeLayout
            android:id="@+id/tableLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView
                android:id="@+id/objet"
                android:hint="@string/objet_alerte"
                android:layout_marginLeft="5dip"
                android:layout_alignParentLeft="true"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="17dp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/temps_passe"
                android:hint="@string/temps_ecoule"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_gravity="right"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_above="@+id/champ1" />
            <TextView
                android:id="@+id/champ1"
                android:hint="@string/champ1"
                android:layout_below="@+id/objet"
                android:layout_marginLeft="10dip"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="13dp" />
            <TextView
                android:id="@+id/champ2"
                android:hint="@string/champ2"
                android:layout_below="@id/champ1"
                android:layout_marginLeft="10dip"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="13dp" />
            <TextView
                android:id="@+id/champ3"
                android:hint="@string/champ3"
                android:layout_below="@+id/champ2"
                android:layout_marginLeft="10dip"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="13dp"/>
            <TextView
                android:id="@+id/niv_alerte"
                android:hint="niv_alerte"
                android:layout_below="@+id/champ3"
                android:layout_marginLeft="10dip"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="13dp"/>
        </RelativeLayout>
    </LinearLayout>
 
</RelativeLayout>
(J'ai laissé en commentaire dans mon activité CorpsApp l'affichage de mon CustomListAdapter à partir d'une chaine string)

Je ne sais pas faire le lien entre la BDD et l'affichage, je ne sais pas comment le gérer, si quelqu'un peut m'aider un peu ce serait cool !
N'hésiter pas à me demander des compléments si je n'ai pas été suffisamment clair !

Simon