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