Implement editing of contacts after being written to the database

4

Hello

I'm adapting a aplicativo that manages contacts with banco de dados interaction.

No aplicativo , I'm inserting and deleting contacts but I'm having trouble implementing método to edit the contacts and save the edited contact in banco de dados .

The método onContextItemSelected is implemented and only this part is missing.

onContextItemSelected method in MainActivity

    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);
    }

DatabaseHandler class with updateContact method

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;
}
}
    
asked by anonymous 30.10.2015 / 03:03

1 answer

3

When you want to edit a contact from some application, what is usually done is the following:

When you click on a contact in a contact list, a new edit (activity or fragment) screen with the contact information clicked opens ( Note: contact information can be passed to the new screen using Bundle ).

The edit screen will have EditTexts populated with the clicked contact information, so in case you want to edit the contact name, simply edit the filled name in EditText . Obviously, this screen also has a Save Changes button and a Cancel Changes button.

So when you click the save changes button, you should do the following:

  • (Ideal) Verify that the data is valid
  • Create a new user using new data in EditText (just create the new contact using new , Do not add to the database yet! );
  • (Ideal, but depends on business logic) Make an appointment with the new user to verify that the new user's data is conflicting with another user already registered.
  • Call the bank update function by passing the new
  • Notify the adapter that the database data has changed.
  • Close the edit screen and return to the previous screen.
  • This is a "simple and general" way to make changes to entries. If you have any questions about implementing one of these steps, or get a simpler way to do so, feel free to share them.

        
    08.11.2015 / 12:52