存储在数据库中后实现编辑联系人

时间:2015-10-30 02:20:51

标签: java android eclipse android-studio android-database

我正在调整管理application互动的联系人的database

application中,我正在插入和删除联系人,但我很难实现联系人methodedit并记录database中编辑的联系人

onContextItemSelected方法已到位且仅缺少该部分。

MainActivity中的onContextItemSelected方法

    public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case EDIT:
            // TODO: Implement editing a contact
            break;
        case DELETE:
            dbHandler.deleteContact(Contacts.get(longClickedItemIndex));
            Contacts.remove(longClickedItemIndex);
            contactAdapter.notifyDataSetChanged();
            break;
    }

    return super.onContextItemSelected(item);
    }

带有updateContact方法的DatabaseHandler类

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "controleAgenda",
        TABLE_CONTACTS = "contatos",
        KEY_ID = "id",
        KEY_NAME = "nome",
        KEY_PHONE = "telefone",
        KEY_ADDRESS = "endereco",
        KEY_EMAIL = "email",
        KEY_IMAGEURI = "imageUri";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + KEY_PHONE + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_EMAIL + " TEXT," + KEY_IMAGEURI + " TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    onCreate(db);
}

public void createContact(Contato contato) {
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_NAME, contato.getName());
    values.put(KEY_PHONE, contato.getPhone());
    values.put(KEY_ADDRESS, contato.getAddress());
    values.put(KEY_EMAIL, contato.getEmail());
    values.put(KEY_IMAGEURI, contato.getImageURI().toString());

    db.insert(TABLE_CONTACTS, null, values);
    db.close();
}

public Contato getContact(int id) {
    SQLiteDatabase db = getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, KEY_NAME, KEY_PHONE, KEY_EMAIL, KEY_ADDRESS, KEY_IMAGEURI }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null );

    if (cursor != null)
        cursor.moveToFirst();

    Contato contato = new Contato(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5)));
    db.close();
    cursor.close();
    return contato;
}

public void deleteContact(Contato contato) {
    SQLiteDatabase db = getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + "=?", new String[] { String.valueOf(contato.getId()) });
    db.close();
}

public int getContactsCount() {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);
    int count = cursor.getCount();
    db.close();
    cursor.close();

    return count;
}

public int updateContact(Contato contato) {
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_NAME, contato.getName());
    values.put(KEY_PHONE, contato.getPhone());
    values.put(KEY_ADDRESS, contato.getAddress());
    values.put(KEY_EMAIL, contato.getEmail());
    values.put(KEY_IMAGEURI, contato.getImageURI().toString());

    int rowsAffected = db.update(TABLE_CONTACTS, values, KEY_ID + "=?", new String[] { String.valueOf(contato.getId()) });
    db.close();

    return rowsAffected;
}

public List<Contato> getAllContacts() {
    List<Contato> contacts = new ArrayList<Contato>();

    SQLiteDatabase db = getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);

    if (cursor.moveToFirst()) {
        do {
            contacts.add(new Contato(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), Uri.parse(cursor.getString(5))));
        }
        while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return contacts;
}
}

0 个答案:

没有答案