在我的应用程序中,我需要在listView中显示联系人列表,此列表已存储在数据库中。
我有这个:
Contact.class:
public class Contact {
private int CTC_ID;
private byte[] CTC_IMAGE;
public String CTC_NOM;
public String CTC_NUMERO;
public Contact(){
}
public Contact(byte[] CTC_IMAGE, String CTC_NOM, String CTC_NUMERO){
this.CTC_IMAGE = CTC_IMAGE;
this.CTC_NOM = CTC_NOM;
this.CTC_NUMERO = CTC_NUMERO;
}
public Contact(String CTC_NOM, String CTC_NUMERO){
this.CTC_NOM = CTC_NOM;
this.CTC_NUMERO = CTC_NUMERO;
}
public int getCTC_ID(){
return CTC_ID;
}
public byte[] getCTC_IMAGE(){return CTC_IMAGE; }
public String getCTC_NOM() {
return CTC_NOM;
}
public String getCTC_NUMERO() {
return CTC_NUMERO;
}
public void setCTC_ID(int CTC_ID) {
this.CTC_ID = CTC_ID;
}
public void setCTC_IMAGE(byte[] CTC_IMAGE) { this.CTC_IMAGE = CTC_IMAGE;}
public void setCTC_NOM(String CTC_NOM) { this.CTC_NOM = CTC_NOM;}
public void setCTC_NUMERO(String CTC_NUMERO) {
this.CTC_NUMERO = CTC_NUMERO;
}
public String toString(){
return "ID : "+CTC_ID+"\nimage : "+CTC_IMAGE+"\nnom : "+CTC_NOM+"\nnumero : "+CTC_NUMERO;
}
}
DatabaseHelper:
public class DatabaseHelper extends SQLiteOpenHelper {
// Logcat tag
private static final String LOG = "DatabaseHelper";
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "kids_phone";
// Table Names
private static final String TABLE_CONTACT = "Contact";
private static final String TABLE_MESSAGE = "Message";
private static final String TABLE_ASSOCIER = "Associer";
// Common column names
private static final String KEY_CTC_ID = "CTC_ID";
private static final String KEY_MES_ID = "MES_ID";
private static final String KEY_ASS_ID = "ASS_ID";
// Contact Table - column names
public static final String KEY_CTC_IMAGE = "CTC_IMAGE";
public static final String KEY_CTC_NOM = "CTC_NOM";
public static final String KEY_CTC_NUMERO = "CTC_NUMERO";
// Message Table - column names
private static final String KEY_MES_TITRE = "MES_TITRE";
private static final String KEY_MES_CONTENU = "MES_CONTENU";
// Associer Table - column names
private static final String KEY_ASS_IMG = "ASS_IMG";
private static final String KEY_ASS_MES_ID = "ASS_MES_ID";
private static final String KEY_ASS_CTC_ID = "ASS_CTC_ID";
// Contact table Create Statements
private static final String CREATE_TABLE_CONTACT = "CREATE TABLE "
+ TABLE_CONTACT + " (" + KEY_CTC_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_CTC_IMAGE
+ " BLOB," + KEY_CTC_NOM + " TEXT," + KEY_CTC_NUMERO + " TEXT NOT NULL);";
// Message table create statement
private static final String CREATE_TABLE_MESSAGE = "CREATE TABLE " + TABLE_MESSAGE
+ " (" + KEY_MES_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_MES_TITRE + " TEXT,"
+ KEY_MES_CONTENU + " TEXT NOT NULL);";
// Associer table create statement
private static final String CREATE_TABLE_ASSOCIER = "CREATE TABLE "
+ TABLE_ASSOCIER + " (" + KEY_ASS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ASS_IMG + " TEXT,"
+ KEY_ASS_MES_ID + " INTEGER," + KEY_ASS_CTC_ID + " INTEGER);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(CREATE_TABLE_CONTACT);
db.execSQL(CREATE_TABLE_MESSAGE);
db.execSQL(CREATE_TABLE_ASSOCIER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ASSOCIER);
// create new tables
onCreate(db);
}
private static SQLiteDatabase bdd;
static ArrayList<Contact> contactListe = new ArrayList<Contact>();
public void open() {
//on ouvre la BDD en écriture
SQLiteDatabase bdd = this.getWritableDatabase();
}
public void close() {
//on ferme l'accès à la BDD
bdd.close();
}
public long insertContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
//Création d'un ContentValues (fonctionne comme une HashMap)
ContentValues values = new ContentValues();
//on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
values.put(KEY_CTC_IMAGE, contact.getCTC_IMAGE());
values.put(KEY_CTC_NOM, contact.getCTC_NOM());
values.put(KEY_CTC_NUMERO, contact.getCTC_NUMERO());
//on insère l'objet dans la BDD via le ContentValues
long CTC_ID = db.insert(TABLE_CONTACT, null, values);
return CTC_ID;
}
public Cursor fetchAllContact() throws SQLException {
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCursor = db.query(TABLE_CONTACT, new String[]{"CTC_ID _id", KEY_CTC_NOM, KEY_CTC_NUMERO}, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public ArrayList<Contact> fetchAllContactDetail() {
SQLiteDatabase db = this.getWritableDatabase();
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);
if (mCursor.moveToFirst()) {
do {
byte[] image = mCursor.getBlob(mCursor.getColumnIndex(KEY_CTC_IMAGE));
String name = mCursor.getString(mCursor.getColumnIndex(KEY_CTC_NOM));
String numero = mCursor.getString(mCursor.getColumnIndex(KEY_CTC_NUMERO));
contactListe.add(new Contact(image, name, numero));
} while (mCursor.moveToNext());
}
return contactListe;
}
}
ContactAdapter:
public class ContactAdapter extends BaseAdapter {
private Context context;
private ArrayList<Contact> contactList = new ArrayList<>();
// Constructor
public ContactAdapter(Context context, ArrayList<Contact> contactList) {
this.context = context;
this.contactList.addAll(contactList);
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
ImageView img;
TextView name;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.contact_item, null);
holder.name = (TextView) row.findViewById(R.id.name);
holder.img = (ImageView) row.findViewById(R.id.img);
row.setTag(holder);
}
else {
holder = (ViewHolder) row.getTag();
}
Contact contact = contactList.get(position);
holder.name.setText(contact.getCTC_NUMERO());
byte[] contactImage = contact.getCTC_IMAGE();
Bitmap bitmap = BitmapFactory.decodeByteArray(contactImage, 0, contactImage.length);
holder.img.setImageBitmap(bitmap);
return row;
}
}
我的主要活动:
public class MainActivity extends AppCompatActivity {
private ListView lstViewContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstViewContact = (ListView) findViewById(R.id.lvContact);
displayContact();
}
public void displayContact() {
//On ouvre la base de données pour écrire dedans
DatabaseHelper dbHelper = new DatabaseHelper(this);
final ContactAdapter contactAdapter = new ContactAdapter(this, dbHelper.fetchAllContactDetail());
lstViewContact.setAdapter(contactAdapter);
}
}
我的数据库中有2个联系人,但是当我启动应用程序时,他没有显示他。
编辑:
测试添加联系人:
public class AjoutContact extends AppCompatActivity implements View.OnClickListener {
EditText recupNom;
EditText recupNumero;
Button ajouter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ajout_contact);
ajouter = (Button) findViewById(R.id.AjoutContact);
recupNom = (EditText) findViewById(R.id.Nom);
recupNumero = (EditText) findViewById(R.id.Numero);
ajouter.setOnClickListener(AjoutContact.this);
}
@Override
public void onClick(View v) {
// get image from drawable
Bitmap image = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte imageInByte[] = stream.toByteArray();
String chaine = recupNom.getText().toString();
String chaine2 = recupNumero.getText().toString();
//Création d'une instance de ma classe MessageBDD
DatabaseHelper dbh = new DatabaseHelper(this);
//Création d'un contact
Contact contact = new Contact(imageInByte, chaine, chaine2);
//On insère le contact que l'on vient de créer
dbh.insertContact(contact);
Toast.makeText(AjoutContact.this, "Vous avez ajouter le Contact suivant : " + contact , Toast.LENGTH_LONG).show();
}
}