我想通过 ItemlongClickListener 从我的列表视图中删除项目。显示项目是我的数据库的列值。数据库查询写在单独的文件中,listview 代码在单独的文件中。我试过了,但我的应用程序停止了。我的 ListView 页面代码如下:
fav_quote.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
Log.w("DELETED", " DELETED");
String position1 = adapt.getItem(position);
Fav del = new Fav(this);
del.deleteEntry(position1);
del.close();
return false;
}
数据库代码
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
onCreate(db);
}
}
public Fav(Context c) {
ourContext = c;
}
public Fav(OnItemLongClickListener onItemLongClickListener) {
Context c1 = null;
ourContext = c1;
// TODO Auto-generated constructor stub
}
public Fav open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close (){
ourHelper.close();
}
public long createEntry(String header, String quote_value) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_HEADER, header);
cv.put(KEY_QUOTE_VALUE, quote_value);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String []{KEY_ROWID, KEY_HEADER, KEY_QUOTE_VALUE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iHeader = c.getColumnIndex(KEY_HEADER);
int iQuote_value = c.getColumnIndex(KEY_QUOTE_VALUE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow)+ "." +c.getString(iHeader)+ ":" +c.getString(iQuote_value)+ ":";
}
return result;
}
public void deleteEntry(String position1) {
// TODO Auto-generated method stub
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + position1 , null);
}