Bonjour,
Je suis en train de développer une application qui doit changer la couleur d'un EditText en fonction de la valeur contenue dans un champ de ma base de données.
J'arrive à comprendre comment insérer les données dans la base, les afficher avec un Cursor qui récupère toutes les données intéressantes(ou du moins selon le cas qui m'intéresse), j'arrive pas à comprendre comment modifier l'affichage en fonction d'une valeur de la bdd.
Mon code est composé de deux classes :
- ma classe BDDAlertequi est chargée de la gestion de la base de données
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 public class BDDAlerte extends Thread { private static final String TAG = "DBAdapter"; //db fields private static final String DATABASE_NAME = "base_alerte_bonnebdd.db"; private static final int DATABASE_VERSION = 1; public static final String TABLE_ALERTE = "table_alerte"; //fields and col numbers public static final String _ID = "_id"; public static final int NUM_COL_ID = 0; public static final String COL_NB_CHAMPS = "nb_champs"; public static final int NUM_COL_NB_CHAMPS =1 ; public static final String COL_ID_CLIENT = "id_client"; public static final int NUM_COL_ID_CLIENT = 2; public static final String COL_MOM_ALERTE = "moment_alerte"; public static final int NUM_COL_MOM_ALERTE = 3; public static final String COL_MOM_MESSAGE = "mom_message"; public static final int NUM_COL_MOM_MESSAGE = 4; public static final String COL_NIV_ALERTE = "niv_alerte"; public static final int NUM_COL_NIV_ALERTE = 5; public static final String COL_OBJ_ALERTE = "obj_alerte"; public static final int NUM_COL_OBJ_ALERTE = 6; public static final String COL_CHAMP1 = "champ1"; public static final int NUM_COL_CHAMP1 = 7; public static final String COL_CHAMP2 = "champ2"; public static final int NUM_COL_CHAMP2 = 8; public static final String COL_CHAMP3 = "champ3"; public static final int NUM_COL_CHAMP3 = 9; public static final String COL_LIEN = "lien"; public static final int NUM_COL_LIEN = 10; //return string des champs public static final String[] ALL_KEYS = new String[]{ _ID, COL_NB_CHAMPS, COL_ID_CLIENT, COL_MOM_ALERTE, COL_MOM_MESSAGE, COL_NIV_ALERTE, COL_OBJ_ALERTE, COL_CHAMP1, COL_CHAMP2, COL_CHAMP3, COL_LIEN}; //creation bdd private static final String CREATE_DATABASE = " CREATE TABLE " + TABLE_ALERTE + "( " +_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+COL_NB_CHAMPS+ " BIGINT, " + COL_ID_CLIENT+ " TEXT, " +COL_MOM_ALERTE+ " DATETIME, " + COL_MOM_MESSAGE + " INTEGER DEFAULT(STRFTIME('%s', 'now')) , " + COL_NIV_ALERTE+" INTEGER, "+COL_OBJ_ALERTE+ " TEXT, " + COL_CHAMP1 + " TEXT, " + COL_CHAMP2 +" TEXT, " + COL_CHAMP3+" TEXT , " + COL_LIEN + " TEXT);"; private final Context context; private DatabaseHelper myDBHelper; private static SQLiteDatabase db; //methodes publiques public BDDAlerte(Context ctx){ this.context= ctx; myDBHelper = new DatabaseHelper(context); } public BDDAlerte open(){ db = myDBHelper.getWritableDatabase(); return this; } public void close(){ myDBHelper.close(); } public static long insertAlerte(String nb_champs, String id_client, String moment_alerte, String niv_alerte, String obj_alerte, String champ1, String champ2, String champ3, String lien){ //creation d'un ContentValues (fonctionne comme HashMap) ContentValues values = new ContentValues(); values.put(COL_NB_CHAMPS, nb_champs); values.put(COL_ID_CLIENT, id_client); values.put(COL_MOM_ALERTE, moment_alerte); values.put(COL_NIV_ALERTE, niv_alerte); values.put(COL_OBJ_ALERTE, obj_alerte); values.put(COL_CHAMP1, champ1); values.put(COL_CHAMP2, champ2); values.put(COL_CHAMP3, champ3); values.put(COL_LIEN, lien); //suppression des vieilles alertes en direct à l'entree des nouvelles suppVieillesAlertes(); return db.insert(TABLE_ALERTE, null, values); } public static boolean deleteRow(long rowId){ String where = _ID +"="+rowId; return db.delete(TABLE_ALERTE, where, null)!=0; } public void deleteAll(){ Cursor c = getAllRows(); long rowId = c.getColumnIndexOrThrow(_ID); if(c.moveToFirst()){ do{ deleteRow(c.getLong((int)rowId)); }while(c.moveToNext()); } c.close(); } //supprime les alertes les plus vieilles quand il y en a plus de 1000 public static void suppVieillesAlertes(){ String order = _ID+ " DESC"; String where = " _ID < ("+BDDAlerte.getMaxId() +"-1999) "; Cursor c = db.query(true, TABLE_ALERTE, ALL_KEYS, where, null, null, null, order, null); long rowId = c.getColumnIndexOrThrow(_ID); if(c.moveToFirst()){ do{ deleteRow(c.getLong((int)rowId)); }while(c.moveToNext()); } c.close(); } public static Cursor getAllRows(){ String order = _ID+ " DESC"; Cursor c = db.query(true, TABLE_ALERTE, ALL_KEYS, null, null, null, null, order, null); if (c!=null){ c.moveToFirst(); } return c; } //compte les alertes public static int countAlertes(){ Cursor mCount= db.rawQuery("select count(*) from TABLE_ALERTE ", null); mCount.moveToFirst(); int count= mCount.getInt(0); mCount.close(); return count; } public static Cursor getAlerteNiveau(int niveau){ String order = _ID+ " DESC"; String where = COL_NIV_ALERTE + " = " + niveau; Cursor c = db.query(true, TABLE_ALERTE, ALL_KEYS, where, null, null, null, order, null); if (c!=null){ c.moveToFirst(); } return c; } public static Cursor getRow (long rowId){ String where = _ID +"="+rowId; Cursor c = db.query(true, TABLE_ALERTE, ALL_KEYS, where, null, null, null, null, null); if(c!=null){ c.moveToFirst(); } return c; } public static Cursor getAlertesRecentes(int secondes){ String order = _ID+ " DESC"; long unixTime = System.currentTimeMillis() / 1000L; String where = COL_MOM_MESSAGE+ " > "+ unixTime + " - "+secondes; Cursor c = db.query(true, TABLE_ALERTE, ALL_KEYS, where, null, null, null, order, null); if(c!=null){ c.moveToFirst(); } return c; } //private helper classes pour la creation et l'upgrading db private static class DatabaseHelper extends SQLiteOpenHelper{ DatabaseHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase _db){ _db.execSQL(CREATE_DATABASE); } @Override public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion){ Log.w(TAG, "Upgrading application's database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data!"); // Destroy old database: _db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALERTE); // Recreate new database: onCreate(_db); } } //recupere valeur derniere alerte public static long getMaxId(){ String query = "SELECT MAX(_ID) AS max_id FROM TABLE_ALERTE"; Cursor cursor = db.rawQuery(query, null); int id = 0; if (cursor.moveToFirst()){ do{ id = cursor.getInt(0); } while(cursor.moveToNext()); } return id; } }
- ma classe MainActivity qui est chargée de rentrer les données dans la base, et de l'affichage dans le ListView :
J'ai de plus un fichier xml pour les items de la ListView :
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 public class MainActivity extends Activity { private DBAdapter myDb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); openDB(); populateListViewFromDB(); int nombrealertes = DBAdapter.countAlertes(); Toast.makeText(getApplicationContext(), "nombre d'entrées : "+ nombrealertes , Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), "id max : " + DBAdapter.getMaxId() , Toast.LENGTH_LONG).show(); } @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); return true; } @Override protected void onDestroy(){ super.onDestroy(); closeDB(); } private void openDB(){ myDb = new DBAdapter(this); myDb.open(); } private void closeDB(){ myDb.close(); } public void ajouterEntree(View v){ new Thread(new Runnable() { @Override public void run() { //displayText("ajout d'une entree"); long newId = myDb.insertAlerte("7", "patapipe,","14:12:22","2", "DFXGSZTEQRDWFHYJQER", "champ1", "champs2", "champs3", "adresse du lien"); Cursor cursor = myDb.getRow(newId); displayRecordSet(cursor); DBAdapter.suppVieillesAlertes(); runOnUiThread(new Runnable() { @Override public void run() { populateListViewFromDB(); } }); } }).start(); } public void supprimerEntrees (View v){ new Thread(new Runnable() { @Override public void run() { //displayText("ajout d'une entree"); long newId = myDb.insertAlerte("7", "patapipe,","14:12:22","4", "DFXGSZTEQRDWFHYkfhgjkmfulkfumgugkumgifuylfhktfQER", "champ1", "champs2", "champs3", "adresse du lien"); Cursor cursor = myDb.getRow(newId); displayRecordSet(cursor); DBAdapter.suppVieillesAlertes(); runOnUiThread(new Runnable() { @Override public void run() { populateListViewFromDB(); } }); } }).start(); } public void afficherEntrees(View v){ /*Cursor cursor = myDb.getAllRows(); displayRecordSet(cursor); populateListViewFromDB();*/ DBAdapter.suppVieillesAlertes(); populateListViewFromDB(); } //vide la liste d'alerte public void viderListView (){ ListView myList= (ListView)findViewById(R.id.listView1); myList.setAdapter(null); } public void displayRecordSet(Cursor cursor){ if(cursor.moveToFirst()) { do{ int _id = cursor.getInt(DBAdapter.NUM_COL_ID); String nb_champs = cursor.getString(DBAdapter.NUM_COL_NB_CHAMPS); String id_client = cursor.getString(DBAdapter.NUM_COL_ID_CLIENT); String mom_alerte = cursor.getString(DBAdapter.NUM_COL_MOM_ALERTE); String mom_message = cursor.getString(DBAdapter.NUM_COL_MOM_MESSAGE); String niv_alerte = cursor.getString(DBAdapter.NUM_COL_NIV_ALERTE); String obj_alerte = cursor.getString(DBAdapter.NUM_COL_OBJ_ALERTE); String champ1 = cursor.getString(DBAdapter.NUM_COL_CHAMP1); String champ2 = cursor.getString(DBAdapter.NUM_COL_CHAMP2); String champ3 = cursor.getString(DBAdapter.NUM_COL_CHAMP3); String lien = cursor.getString(DBAdapter.NUM_COL_LIEN); }while(cursor.moveToNext()); } cursor.close(); } public void populateListViewFromDB (){ new Thread(new Runnable() { @Override public void run() { final Cursor cursor = myDb.getAllRows(); final String[] fromFieldNames = new String[]{ DBAdapter._ID, DBAdapter.COL_ID_CLIENT, DBAdapter.COL_MOM_MESSAGE, DBAdapter.COL_NIV_ALERTE, DBAdapter.COL_OBJ_ALERTE, DBAdapter.COL_CHAMP1, DBAdapter.COL_CHAMP2, DBAdapter.COL_CHAMP3}; final int [] toViewIDs = new int[]{R.id.id_alerte, R.id.id_client, R.id.mom_message, R.id.niv_alerte, R.id.objet, R.id.champ1, R.id.champ2, R.id.champ3}; runOnUiThread(new Runnable() { @Override public void run() { SimpleCursorAdapter myCursorAdapter; myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.row_list, cursor, fromFieldNames, toViewIDs, 0); ListView myList= (ListView)findViewById(R.id.listView1); myList.setAdapter(myCursorAdapter); } }); } }).start(); } }
J'aimerais changer la valeur du TextView couleur_alerte en fonction du niveau d'alerte contenu dans mon champ COL_NIV_ALERTE de ma base.
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 <?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/id_alerte" android:layout_marginLeft="5dip" android:layout_alignParentLeft="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle="bold" /> <TextView android:id="@+id/mom_message" android:layout_marginLeft="5dip" android:layout_below="@id/id_alerte" android:layout_alignParentLeft="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle="bold" /> <TextView android:id="@+id/objet" android:singleLine="true" android:layout_marginLeft="5dip" android:layout_alignParentLeft="true" android:layout_below="@id/mom_message" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="17dp" android:textStyle="bold" /> <TextView android:id="@+id/id_client" android:layout_marginLeft="5dip" android:layout_below="@id/objet" android:layout_alignParentLeft="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="17dp" android:textStyle="bold" /> <TextView android:id="@+id/champ1" android:layout_below="@+id/id_client" android:layout_marginLeft="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="13dp" /> <TextView android:id="@+id/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: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>
Je ne sais pas comment récupérer la valeur de la base pour changer la couleur de mon TextView dynamiquement.
Si j'ai pas été suffisamment clair ou si vous avez besoin d'autres infos demandez moi !
Simon
Partager