我的活动代码
请帮助我如何删除android studio中连接到数据库的列表视图中的项。我的代码是:
mainlistview.setOnTimeLongClickListener(新建adapterView.onTimeLongClickListener(){
public boolean ontemlongclick(adapterview parent,view view,final int position,final long id){
final alertDialog.builder b=新建alertDialog.builder(userlist.this);
b.seticon(android.r.drawable.ic_dialog_alert);
b.setMessage(“Ingin Menghapus数据?”)(二)
b.setPositiveButton(“是”,
新建DialogInterface.onClickListener(){
public void onclick(dialoginterface dialog,int whichbutton){
idtable=idlist.get(位置);
删除(位置);
userlist.this.listadapter.notifyDataSetChanged();
infopokok info=新建infopokok();
信息集id(idtable);
system.out.println(“id:”+info.getid());
infopokokdao.deleteinfopokok(信息);
}
});
b.setNegativeButton(“tidak”,
新建DialogInterface.onClickListener(){
public void onclick(dialoginterface dialog,int whichbutton){
dialog.cancel();
}
});
b.表演();
返回真值;
}
});
道:
public void deleteinfopokok(infopokok infopokok){
字符串id=infopokok.getid()+“”;
long delete id=数据库.删除(mysqlitehelper.table_info_pokok,mysqlitehelper.column_id
+“=?”,新字符串[]{id});
光标光标=database.query(mysqlitehelper.table_info_pokok,
allcolumns,mysqlitehelper.column_id+“=”+deleteid,空,
空,空,空);
cursor.moveToFirst();
cursor.close();
}
最佳答案
在displayActivity.java中
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataBaseHandler db = new DataBaseHandler(DisplayImageActivity.this);
Log.d("Delete Image: ", "Deleting.....");
db.deleteCloth(new Cloth(imageId));
Intent i = new Intent(DisplayImageActivity.this, MainActivity.class);
startActivity(i);
finish();
}
});
然后在databasehandler.java中
public void deleteCloth(Cloth cloth) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CLOTHES, KEY_ID + " = ?", new String[] { String.valueOf(cloth.getID()) });
db.close();
}