public class DataBaseHandler extends SQLiteOpenHelper {
// Version de la BD
private static final int DATA_BASE_VERSION = 1;
// Nom de la BD
private static final String DATA_BASE_NAME = "contactsManager";
// Nom de la table Contacts
private static final String TABLE_CONTACTS = "contacts";
// Colonnes de la table Contacts
private static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_PH_NB = "phone_number";
private SQLiteDatabase maBaseDeDonnees;
private DataBaseHandler baseHandler;
// méthode permettant d'ouvrir la base en écriture
public void open() {
maBaseDeDonnees = baseHandler.getWritableDatabase();
}
public DataBaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version) {
super(context, DATA_BASE_NAME, null, DATA_BASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PH_NB + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
onCreate(db);
}
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Nom du contact
values.put(KEY_PH_NB, contact.getPhone_number()); // Tel du contact
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Fermer la connection à la base de données
}
Contact getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NB }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null) // il existe un contact avec l'id spécifié
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
db.close();
return contact;
}
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhone_number(cursor.getString(2));
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}
public int removeContact(String name) {
return maBaseDeDonnees.delete(DATA_BASE_NAME, KEY_NAME + "LIKE" + name,
null);
}
public int updateContact(Contact c) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues val = new ContentValues();
val.put(KEY_NAME, c.getName());
val.put(KEY_PH_NB, c.getPhone_number());
return db.update(TABLE_CONTACTS, val, KEY_ID + "=?",
new String[] { String.valueOf(c.getId()) });
}
public void deleteContact(Contact c) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + "=?",
new String[] { String.valueOf(c.getId()) });
db.close();
}
public int getContactsNumber() {
String req = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery(req, null);
c.close();
return c.getCount();
}
} public class MainActivity extends Activity {
String nom, tel;
// Version de la BD
private static final int DATA_BASE_VERSION = 1;
// Nom de la BD
private static final String DATA_BASE_NAME = "contactsManager";
DataBaseHandler db = new DataBaseHandler(this, DATA_BASE_NAME, null, DATA_BASE_VERSION);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
ListView lv = (ListView) findViewById(R.id.listView1);
String[] values = new String[] { "Afficher tous les contacts",
"Ajouter un contact",
"Modifier un contact",};
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.simplerow,values);
// Associer la listView XML à java //Affichage de tous les contacts
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//hjhgjgjghgv lljklnkjnj if (position==0) {
EditText edt = (EditText) findViewById(R.id.editText);
String resultat = String.valueOf(db.getAllContacts());
edt.setText(String.valueOf(resultat));
}else if (position==1){
EditText ed1 = (EditText) findViewById(R.id.editnom);
EditText ed2 = (EditText) findViewById(R.id.editphone);
nom = ed1.getText().toString();
tel = ed2.getText().toString();
db.getWritableDatabase();
db.getReadableDatabase();
Contact nouveauContact = new Contact();
nouveauContact.setName(nom);
nouveauContact.setPhone_number(tel);
Log.d("Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "Id: " + cn.getId() + " ,Name: " + cn.getName() + " ,Phone: " + cn.getPhone_number();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
}
});
}