· 9 years ago · Jan 16, 2017, 12:16 PM
1public class Contact {
2
3 private int CTC_ID;
4 private byte[] CTC_IMAGE;
5 public String CTC_NOM;
6 public String CTC_NUMERO;
7
8 public Contact(){
9 }
10
11 public Contact(byte[] CTC_IMAGE, String CTC_NOM, String CTC_NUMERO){
12 this.CTC_IMAGE = CTC_IMAGE;
13 this.CTC_NOM = CTC_NOM;
14 this.CTC_NUMERO = CTC_NUMERO;
15 }
16 public Contact(String CTC_NOM, String CTC_NUMERO){
17 this.CTC_NOM = CTC_NOM;
18 this.CTC_NUMERO = CTC_NUMERO;
19 }
20
21
22
23 public int getCTC_ID(){
24 return CTC_ID;
25 }
26 public byte[] getCTC_IMAGE(){return CTC_IMAGE; }
27 public String getCTC_NOM() {
28 return CTC_NOM;
29 }
30 public String getCTC_NUMERO() {
31 return CTC_NUMERO;
32 }
33
34 public void setCTC_ID(int CTC_ID) {
35 this.CTC_ID = CTC_ID;
36 }
37 public void setCTC_IMAGE(byte[] CTC_IMAGE) { this.CTC_IMAGE = CTC_IMAGE;}
38 public void setCTC_NOM(String CTC_NOM) { this.CTC_NOM = CTC_NOM;}
39 public void setCTC_NUMERO(String CTC_NUMERO) {
40 this.CTC_NUMERO = CTC_NUMERO;
41 }
42
43
44 public String toString(){
45 return "ID : "+CTC_ID+"nimage : "+CTC_IMAGE+"nnom : "+CTC_NOM+"nnumero : "+CTC_NUMERO;
46 }
47}
48
49public class DatabaseHelper extends SQLiteOpenHelper {
50
51 // Logcat tag
52 private static final String LOG = "DatabaseHelper";
53
54 // Database Version
55 private static final int DATABASE_VERSION = 1;
56
57 // Database Name
58 private static final String DATABASE_NAME = "kids_phone";
59
60 // Table Names
61 private static final String TABLE_CONTACT = "Contact";
62 private static final String TABLE_MESSAGE = "Message";
63 private static final String TABLE_ASSOCIER = "Associer";
64
65 // Common column names
66 private static final String KEY_CTC_ID = "CTC_ID";
67 private static final String KEY_MES_ID = "MES_ID";
68 private static final String KEY_ASS_ID = "ASS_ID";
69
70 // Contact Table - column names
71 public static final String KEY_CTC_IMAGE = "CTC_IMAGE";
72 public static final String KEY_CTC_NOM = "CTC_NOM";
73 public static final String KEY_CTC_NUMERO = "CTC_NUMERO";
74
75 // Message Table - column names
76 private static final String KEY_MES_TITRE = "MES_TITRE";
77 private static final String KEY_MES_CONTENU = "MES_CONTENU";
78
79 // Associer Table - column names
80 private static final String KEY_ASS_IMG = "ASS_IMG";
81 private static final String KEY_ASS_MES_ID = "ASS_MES_ID";
82 private static final String KEY_ASS_CTC_ID = "ASS_CTC_ID";
83
84 // Contact table Create Statements
85 private static final String CREATE_TABLE_CONTACT = "CREATE TABLE "
86 + TABLE_CONTACT + " (" + KEY_CTC_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_CTC_IMAGE
87 + " BLOB," + KEY_CTC_NOM + " TEXT," + KEY_CTC_NUMERO + " TEXT NOT NULL);";
88
89 // Message table create statement
90 private static final String CREATE_TABLE_MESSAGE = "CREATE TABLE " + TABLE_MESSAGE
91 + " (" + KEY_MES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_MES_TITRE + " TEXT,"
92 + KEY_MES_CONTENU + " TEXT NOT NULL);";
93
94 // Associer table create statement
95 private static final String CREATE_TABLE_ASSOCIER = "CREATE TABLE "
96 + TABLE_ASSOCIER + " (" + KEY_ASS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ASS_IMG + " TEXT,"
97 + KEY_ASS_MES_ID + " INTEGER," + KEY_ASS_CTC_ID + " INTEGER);";
98
99 public DatabaseHelper(Context context) {
100 super(context, DATABASE_NAME, null, DATABASE_VERSION);
101 }
102
103 @Override
104 public void onCreate(SQLiteDatabase db) {
105
106 // creating required tables
107 db.execSQL(CREATE_TABLE_CONTACT);
108 db.execSQL(CREATE_TABLE_MESSAGE);
109 db.execSQL(CREATE_TABLE_ASSOCIER);
110 }
111
112 @Override
113 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
114 // on upgrade drop older tables
115 db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT);
116 db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGE);
117 db.execSQL("DROP TABLE IF EXISTS " + TABLE_ASSOCIER);
118
119 // create new tables
120 onCreate(db);
121 }
122
123 private static SQLiteDatabase bdd;
124 static ArrayList<Contact> contactListe = new ArrayList<Contact>();
125
126 public void open() {
127 //on ouvre la BDD en écriture
128 SQLiteDatabase bdd = this.getWritableDatabase();
129 }
130
131 public void close() {
132 //on ferme l'accès à la BDD
133 bdd.close();
134 }
135
136
137 public long insertContact(Contact contact) {
138 SQLiteDatabase db = this.getWritableDatabase();
139 //Création d'un ContentValues (fonctionne comme une HashMap)
140 ContentValues values = new ContentValues();
141 //on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
142 values.put(KEY_CTC_IMAGE, contact.getCTC_IMAGE());
143 values.put(KEY_CTC_NOM, contact.getCTC_NOM());
144 values.put(KEY_CTC_NUMERO, contact.getCTC_NUMERO());
145 //on insère l'objet dans la BDD via le ContentValues
146 long CTC_ID = db.insert(TABLE_CONTACT, null, values);
147
148 return CTC_ID;
149 }
150
151 public Cursor fetchAllContact() throws SQLException {
152 SQLiteDatabase db = this.getWritableDatabase();
153 Cursor mCursor = db.query(TABLE_CONTACT, new String[]{"CTC_ID _id", KEY_CTC_NOM, KEY_CTC_NUMERO}, null, null, null, null, null);
154
155 if (mCursor != null) {
156 mCursor.moveToFirst();
157 }
158 return mCursor;
159 }
160
161 public ArrayList<Contact> fetchAllContactDetail() {
162 SQLiteDatabase db = this.getWritableDatabase();
163 Cursor mCursor = db.query(TABLE_CONTACT, new String[]{"CTC_ID _id", KEY_CTC_IMAGE, KEY_CTC_IMAGE, KEY_CTC_NOM, KEY_CTC_NUMERO}, null, null, null, null, null);
164
165 if (mCursor.moveToFirst()) {
166 do {
167 byte[] image = mCursor.getBlob(mCursor.getColumnIndex(KEY_CTC_IMAGE));
168 String name = mCursor.getString(mCursor.getColumnIndex(KEY_CTC_NOM));
169 String numero = mCursor.getString(mCursor.getColumnIndex(KEY_CTC_NUMERO));
170 contactListe.add(new Contact(image, name, numero));
171 } while (mCursor.moveToNext());
172 }
173 return contactListe;
174 }
175}
176
177public class ContactAdapter extends BaseAdapter {
178
179 private Context context;
180 private ArrayList<Contact> contactList = new ArrayList<>();
181
182
183 // Constructor
184 public ContactAdapter(Context context, ArrayList<Contact> contactList) {
185 this.context = context;
186 this.contactList.addAll(contactList);
187 }
188 @Override
189 public int getCount() {
190 return contactList.size();
191 }
192
193 @Override
194 public Object getItem(int position) {
195 return contactList.get(position);
196 }
197
198 @Override
199 public long getItemId(int position) {
200 return position;
201 }
202
203 private class ViewHolder{
204 ImageView img;
205 TextView name;
206 }
207
208 @Override
209 public View getView(int position, View view, ViewGroup viewGroup) {
210
211 View row = view;
212 ViewHolder holder = new ViewHolder();
213
214 if(row == null){
215 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
216 row = inflater.inflate(R.layout.contact_item, null);
217
218 holder.name = (TextView) row.findViewById(R.id.name);
219 holder.img = (ImageView) row.findViewById(R.id.img);
220 row.setTag(holder);
221 }
222 else {
223 holder = (ViewHolder) row.getTag();
224 }
225
226 Contact contact = contactList.get(position);
227
228 holder.name.setText(contact.getCTC_NUMERO());
229
230 byte[] contactImage = contact.getCTC_IMAGE();
231 Bitmap bitmap = BitmapFactory.decodeByteArray(contactImage, 0, contactImage.length);
232 holder.img.setImageBitmap(bitmap);
233
234 return row;
235 }
236}
237
238public class MainActivity extends AppCompatActivity {
239private ListView lstViewContact;
240@Override
241protected void onCreate(Bundle savedInstanceState) {
242 super.onCreate(savedInstanceState);
243 setContentView(R.layout.activity_main);
244
245 lstViewContact = (ListView) findViewById(R.id.lvContact);
246
247 displayContact();
248
249public void displayContact() {
250
251 //On ouvre la base de données pour écrire dedans
252 DatabaseHelper dbHelper = new DatabaseHelper(this);
253 final ContactAdapter contactAdapter = new ContactAdapter(this, dbHelper.fetchAllContactDetail());
254 lstViewContact.setAdapter(contactAdapter);
255}