· 9 years ago · Jan 20, 2017, 08:14 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}
261
262public class MainActivity extends Activity implements SwipeActionAdapter.SwipeActionListener {
263
264 public static int OVERLAY_PERMISSION_REQ_CODE = 1234;
265 protected SwipeActionAdapter mAdapter;
266 private ListView lstViewContact;
267
268 @Override
269 protected void onCreate(Bundle savedInstanceState) {
270 super.onCreate(savedInstanceState);
271 setContentView(R.layout.activity_main);
272 lstViewContact = (ListView) findViewById(R.id.lvContact);
273
274 displayContact();
275}
276 public void displayContact() {
277
278 //On ouvre la base de données pour écrire dedans
279 DatabaseHelper dbHelper = new DatabaseHelper(this);
280 final ContactAdapter contactAdapter = new ContactAdapter(this, dbHelper.getListContact());
281 mAdapter = new SwipeActionAdapter(contactAdapter);
282 lstViewContact.setAdapter(mAdapter);
283 }
284
285 @Override
286 public boolean hasActions(int position, SwipeDirection direction) {
287 if (direction.isLeft()) return true;
288 if (direction.isRight()) return true;
289 return false;
290 }
291
292 @Override
293 public boolean shouldDismiss(int position, SwipeDirection direction) {
294 return direction == SwipeDirection.DIRECTION_NORMAL_LEFT;
295 }
296
297 @Override
298 public void onSwipe(int[] positionList, SwipeDirection[] directionList) {
299 for (int i = 0; i < positionList.length; i++) {
300 SwipeDirection direction = directionList[i];
301 int position = positionList[i];
302 String dir = "";
303
304 switch (direction) {
305 case DIRECTION_FAR_LEFT:
306 dir = "Far left";
307 break;
308 case DIRECTION_NORMAL_LEFT:
309 dir = "Left";
310 break;
311 case DIRECTION_FAR_RIGHT:
312 dir = "Far right";
313 break;
314 case DIRECTION_NORMAL_RIGHT:
315 AlertDialog.Builder builder = new AlertDialog.Builder(this);
316 builder.setTitle("Test Dialog").setMessage("You swiped right").create().show();
317 dir = "Right";
318 break;
319 }
320 Toast.makeText(
321 this,
322 dir + " swipe Action triggered on " + mAdapter.getItem(position),
323 Toast.LENGTH_SHORT
324 ).show();
325 mAdapter.notifyDataSetChanged();
326 }
327 }
328}
329
330E/AndroidRuntime: FATAL EXCEPTION: main
331 Process: info.picse.phone_kids, PID: 24155
332 java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.view.View$OnTouchListener.onTouch(android.view.View, android.view.MotionEvent)' on a null object reference
333 at com.wdullaer.swipeactionadapter.SwipeViewGroup.onInterceptTouchEvent(SwipeViewGroup.java:174)
334 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1961)
335 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
336 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
337 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
338 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
339 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
340 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
341 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
342 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
343 at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
344 at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
345 at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2394)
346 at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
347 at android.app.Activity.dispatchTouchEvent(Activity.java:2809)
348 at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2355)
349 at android.view.View.dispatchPointerEvent(View.java:8667)
350 at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4255)
351 at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4121)
352 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3574)
353 at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3627)
354 at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3593)
355 at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3710)
356 at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3601)
357 at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3767)
358 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3574)
359 at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3627)
360 at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3593)
361 at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3601)
362 at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3574)
363 at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5939)
364 at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5913)
365 at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5884)
366 at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6029)
367 at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
368 at android.os.MessageQueue.nativePollOnce(Native Method)
369 at android.os.MessageQueue.next(MessageQueue.java:143)
370 at android.os.Looper.loop(Looper.java:122)
371 at android.app.ActivityThread.main(ActivityThread.java:5270)
372 at java.lang.reflect.Method.invoke(Native Method)
373 at java.lang.reflect.Method.invoke(Method.java:372)
374 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:915)
375 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:710)