这是我的主要课程
public class HomeScreenActivity extends Activity {
private Button contactButton;
private Button groupContactButton;
private Button historyButton;
private Button optionsButton;
private ListView contactsView;
public static int selectedContactIndex = -1;
DatabaseHandler db;
List<Contact> contactList;
ArrayAdapter<Contact> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
contactButton = (Button) findViewById(R.id.contactButton);
contactButton.setSelected(true);
groupContactButton = (Button) findViewById(R.id.groupContactButton);
historyButton = (Button) findViewById(R.id.historyButton);
optionsButton = (Button) findViewById(R.id.optionsButton);
contactsView = (ListView) findViewById(R.id.contactsView);
// Set up contact adaptor so the contact list can be viewed in the
// homescreen
db = new DatabaseHandler(this);
contactList = db.getAllContacts();
adapter = new ArrayAdapter<Contact>(this,
R.layout.home_screen_contacts_view, contactList);
contactsView.setAdapter(adapter);
adapter.setNotifyOnChange(true);
// Set Listeners
contactButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setSelected(true);
groupContactButton.setSelected(false);
historyButton.setSelected(false);
}
});
groupContactButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setSelected(true);
historyButton.setSelected(false);
contactButton.setSelected(false);
}
});
historyButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setSelected(true);
groupContactButton.setSelected(false);
contactButton.setSelected(false);
}
});
optionsButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
openOptionsMenu();
}
});
contactsView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
selectedContactIndex = (int) arg3;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.viewContactDetails:
return true;
case R.id.editContactDetails:
if (selectedContactIndex == -1) {// When no contact is selected,
// inform user.
makeAToast("Please select a contact to edit!");
return true;
}
startActivity(new Intent(this, EditContactActivity.class));
return true;
case R.id.newContact:
startActivity(new Intent(this, NewContactActivity.class));
return true;
case R.id.deleteContact:
if (selectedContactIndex == -1) {// When no contact is selected,
// inform user.
makeAToast("Please select a contact to delete!");
return true;
}
// Create an alert asking for contact deletion confirmation.
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this,
AlertDialog.THEME_DEVICE_DEFAULT_DARK);
myAlertDialog.setTitle("Confirm Contact Deletion");
myAlertDialog.setMessage("Are you sure you want to delete "
+ db.getContact(selectedContactIndex).getFullName() +"?"); //ADD THE CONTACT NAME
// Set listener for when they press yes, delete contact and inform
// user.
myAlertDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
db.deleteContact(db.getContact(selectedContactIndex + 1));
makeAToast("Contact successfully deleted.");
Log.d("Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "Id: "+cn.getID()+" ,First Name: " + cn.getFirstName()
+" ,Last Name: " + cn.getLastName()
+" ,Mobile Number: " + cn.getMobileNumber()
+" ,Home Number: " + cn.getHomeNumber()
+" ,Work Number: " + cn.getWorkNumber()
+" ,Home Address: " + cn.getHomeAddress()
+" ,Email Address: " + cn.getEmailAddress()
+" ,Work Address: " + cn.getWorkAddress()
+" ,Contact Notes: " + cn.getContactNotes();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
});
这是我的数据库处理程序类:
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "contactsManager";
// Contacts table name
private static final String TABLE_CONTACTS = "contacts";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_FIRST_NAME = "first_name";
private static final String KEY_LAST_NAME = "last_name";
private static final String KEY_MOB_NO = "mobile_number";
private static final String KEY_HOME_NO = "home_number";
private static final String KEY_WORK_NO = "work_number";
private static final String KEY_HOME_ADDR = "home_address";
private static final String KEY_EMAIL_ADDR = "email_address";
private static final String KEY_WORK_ADDR = "work_address";
private static final String KEY_NOTES = "notes";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_FIRST_NAME + " TEXT," + KEY_LAST_NAME + " TEXT,"
+ KEY_MOB_NO + " TEXT," + KEY_HOME_NO + " TEXT," + KEY_WORK_NO
+ " TEXT," + KEY_HOME_ADDR + " TEXT," + KEY_EMAIL_ADDR
+ " TEXT," + KEY_WORK_ADDR + " TEXT," + KEY_NOTES + " TEXT"
+ ");";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, Contact.totalContacts);
values.put(KEY_FIRST_NAME, contact.getFirstName());
values.put(KEY_LAST_NAME, contact.getLastName());
values.put(KEY_MOB_NO, contact.getMobileNumber());
values.put(KEY_HOME_NO, contact.getHomeNumber());
values.put(KEY_WORK_NO, contact.getWorkNumber());
values.put(KEY_HOME_ADDR, contact.getHomeAddress());
values.put(KEY_EMAIL_ADDR, contact.getEmailAddress());
values.put(KEY_WORK_ADDR, contact.getWorkAddress());
values.put(KEY_NOTES, contact.getContactNotes());
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
// Getting single contact
Contact getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_FIRST_NAME, KEY_LAST_NAME, KEY_MOB_NO, KEY_HOME_NO,
KEY_WORK_NO, KEY_HOME_ADDR, KEY_EMAIL_ADDR, KEY_WORK_ADDR,
KEY_NOTES }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3),
cursor.getString(4), cursor.getString(5), cursor.getString(6),
cursor.getString(7), cursor.getString(8), cursor.getString(9));
return contact;
}
// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setFirstName(cursor.getString(1));
contact.setLastName(cursor.getString(2));
contact.setMobileNumber(cursor.getString(3));
contact.setHomeNumber(cursor.getString(4));
contact.setWorkNumber(cursor.getString(5));
contact.setHomeAddress(cursor.getString(6));
contact.setEmailAddress(cursor.getString(7));
contact.setWorkAddress(cursor.getString(8));
contact.setContactNotes(cursor.getString(9));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FIRST_NAME, contact.getFirstName());
values.put(KEY_LAST_NAME, contact.getLastName());
values.put(KEY_MOB_NO, contact.getMobileNumber());
values.put(KEY_HOME_NO, contact.getHomeNumber());
values.put(KEY_WORK_NO, contact.getWorkNumber());
values.put(KEY_HOME_ADDR, contact.getHomeAddress());
values.put(KEY_EMAIL_ADDR, contact.getEmailAddress());
values.put(KEY_WORK_ADDR, contact.getWorkAddress());
values.put(KEY_NOTES, contact.getContactNotes());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
// Deleting single contact
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
// Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
现在,当我删除联系人时,不仅没有更新主要联系人屏幕中的列表视图,而且当我将所有联系人写入日志cat时,联系人甚至从未从数据库中删除。关于为什么会这样的任何想法?