如何从数据库中删除listview选择的possition id值我的代码只是listview位置值,它不等于数据库值我想从listview中删除listview所选位置值也从数据库中显示所选项目的KEY_TIME值在toast中如何这样做??
msglist.setOnItemLongClickListener(new OnItemLongClickListener() {
// setting onItemLongClickListener and passing the position to the function
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
DatabaseHandler db = new DatabaseHandler(MsgActivity.this);
Log.d("LOGInG VALUE", "Value: " + String.valueOf(arg3));
db.Delete_Contact(position);
return true;
}
});
public class DatabaseHandler extends SQLiteOpenHelper {
String CREATE_INBOX_TABLE = "CREATE TABLE " + TABLE_INBOX + "("
+ KEY_INBOXID + " INTEGER PRIMARY KEY," + KEY_MSG + " TEXT,"+ KEY_TIME + " TEXT" + ")";
public void Delete_Contact(int id) {
// String string =String.valueOf(id);
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_INBOX, KEY_INBOXID + " =? ", new String[] {
String.valueOf(id) });
db.close();
}
}
答案 0 :(得分:1)
DataBaseManager data = new DataBaseManager(context.getApplicationContext());
String QUERY = "SELECT * FROM Cart Where id= "your id";
Cursor cursor = data.selectQuery(QUERY);
int count = cursor.getCount();
if (count <= 0) {
}
else {
data.Delete("YOUR DATABASE NAME", "YOUR ID");
list.remove(position);
notifyDataSetChanged();
}
DatabaseMangerClass
public class DataBaseManager extends SQLiteOpenHelper {
// The Android's default system path of your application database.
@SuppressLint("SdCardPath")
private static String DB_PATH = "data/data/com.Salsoft.pharmapacks/";
private static String DB_NAME = "Cart.sqlite";
private SQLiteDatabase myDataBase;
private SQLiteDatabase myData;
private Context myContext;
// /data/data/com.salsoft.savingdata/db/SavingData.sqlite
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DataBaseManager(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
Boolean isSDPresent = android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED);
if (isSDPresent) {
// yes SD-card is present
} else {
// Sorry
}
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
File directory = new File(DB_PATH);
directory.mkdirs();
CopyFiles();
}
}
private void CopyFiles() {
try {
InputStream is = myContext.getAssets().open(DB_NAME);
File outfile = new File(DB_PATH, DB_NAME);
outfile.getParentFile().mkdirs();
outfile.createNewFile();
if (is == null) {
throw new RuntimeException("stream is null");
} else {
FileOutputStream out = new FileOutputStream(outfile);
byte buf[] = new byte[128];
do {
int numread = is.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
is.close();
out.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
public void insert(String table, String num, ContentValues content) {
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
myData.insert(table, num, content);
}
public void update(String tablename, ContentValues content, String productid) {
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
myData.update(tablename, content, "productid = ?",
new String[] { productid });
}
public void Delete(String tablename, String productid) {
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
myData.delete(tablename, "productid = ?", new String[] { productid });
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// ---retrieve records---
public Cursor selectQuery(String query) throws SQLException {
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
Cursor mCursor = myData.rawQuery(query, null);
mCursor.moveToFirst();
myData.close();
return mCursor;
}
// //////// For Insert And Update Data ////////
public void insert_update(String query) throws SQLException {
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
myData.execSQL(query);
myData.close();
}
}
答案 1 :(得分:0)
将此添加到您的DatabaseManager
:
public void DeleteByName(String Name){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(tableName , columnName , selectionArgs);
db.close();
}