I have sqlite
data represented on a ListView
by a CustomListAdapter
.On clicking a row an alert dialogue
pops up that prompts the user to delete the single row from sqlite
in my activity:
private void deleteDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MyCart.this);
alertDialog.setCancelable(false);
alertDialog.setMessage("Delete item?");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myDb.deleteSingleContact(toString());
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
On my DBHelper.java:
public void deleteSingleContact(String title){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(CONTACTS_TABLE_NAME, CONTACTS_COLUMN_TITLE + "=?", new String[]{title});
//KEY_NAME is a column name
}
However the above code doesn't delete anything.I guess its the function i havent done correctly.Any suggestions?
Full DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_TITLE = "title";
public static final String CONTACTS_COLUMN_AMOUNT = "amount";
public static final String CONTACTS_COLUMN_DESC = "description";
private HashMap hp;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table contacts " +
"(id integer primary key, title text,amount float,description text)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
public boolean insertContact(String title, float amount, String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("title", title);
contentValues.put("amount", amount);
contentValues.put("description", description);
db.insert("contacts", null, contentValues);
return true;
}
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from contacts where id=" + id + "", null);
return res;
}
public int numberOfRows() {
String countQuery = "SELECT * FROM " + CONTACTS_TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int cnt = cursor.getCount();
cursor.close();
return cnt;
}
public boolean updateContact(Integer id, String title, float amount, String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("title", title);
contentValues.put("amount", amount);
contentValues.put("description", description);
db.update("contacts", contentValues, "id = ? ", new String[]{Integer.toString(id)});
return true;
}
public void deleteContact() {
SQLiteDatabase db = this.getWritableDatabase();
db.delete("contacts", null, null);
db.close();
}
public void deleteSingleContact(String title){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(CONTACTS_TABLE_NAME, CONTACTS_COLUMN_TITLE + "=?", new String[]{title});
//KEY_NAME is a column name
}
public boolean checkForTables() {
boolean hasRows = false;
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM " + CONTACTS_TABLE_NAME, null);
cursor.moveToFirst();
int count = cursor.getInt(0);
if(count > 0)
hasRows = true;
db.close();
return hasRows;
}
public ArrayList<ContactListItems> getAllContacts() {
ArrayList<ContactListItems> contactList = new ArrayList<>();
hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from contacts", null);
res.moveToFirst();
while (!res.isAfterLast()) {
ContactListItems contactListItems = new ContactListItems();
contactListItems.setTitle(res.getString(res
.getColumnIndex("title")));
contactListItems.setAmount(res.getFloat(res
.getColumnIndex("amount")));
contactListItems.setDescription(res.getString(res
.getColumnIndex("description")));
contactList.add( contactListItems);
res.moveToNext();
}
res.close();
return contactList;
}
public int getTotalOfAmount(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c=db.rawQuery("SELECT SUM(amount) FROM " +CONTACTS_TABLE_NAME,null);
c.moveToFirst();
int i=c.getInt(0);
c.close();
return i;
}
}
Aucun commentaire:
Enregistrer un commentaire