Monday, October 22, 2018

How to Delete item in ListView and Database - Android Studio

My Activity Code




Please help me how to delete item in list view that connect to database in Android Studio. This my code :



mainListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

public boolean onItemLongClick(AdapterView parent, View view, final int position, final long id) {

final AlertDialog.Builder b = new AlertDialog.Builder(UserList.this);
b.setIcon(android.R.drawable.ic_dialog_alert);
b.setMessage("Ingin menghapus data?");

b.setPositiveButton("Ya",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

IDTable = IDList.get(position);
userList.remove(position);
UserList.this.listAdapter.notifyDataSetChanged();

InfoPokok info = new InfoPokok();
info.setId(IDTable);

System.out.println("ID : " + info.getId());
infoPokokDao.deleteInfoPokok(info);
}
});
b.setNegativeButton("Tidak",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});


b.show();
return true;
}
});


DAO :



public void deleteInfoPokok(InfoPokok infoPokok) {
String id = infoPokok.getId() + "";


long deleteId = database.delete(MySQLiteHelper.TABLE_INFO_POKOK, MySQLiteHelper.COLUMN_ID
+ " =?", new String[]{id});
Cursor cursor = database.query(MySQLiteHelper.TABLE_INFO_POKOK,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + deleteId, null,
null, null, null);
cursor.moveToFirst();
cursor.close();
}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...