I am trying to implement ListView with Delete functionality to delete item from the listview. I am successful to delete but failed to refresh the listview after deletetion of an item from the database.
Actually, Click on listitem, i am displaying AlertBox for "Delete" and "Cancel" action, on clicking "Delete", item should be removed from the database and as well as from the listview and listview should be refreshed. I have also used notifyDataSetChanged()
method.
lview = (ListView) findViewById(R.id.lview);
adapter = new ListView_CustomAdapter(this, listitemDisplay);
lview.setAdapter(adapter);
lview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Show_Alert_box(v.getContext(),"Please select action.",position);
}
});
and the code for Show_Alert_box:
public void Show_Alert_box(Context context, String message,int position)
{
final int pos = position;
final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(getString(R.string.app_name_for_alert_Dialog));
alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
try
{
db.open();
String[] whereArgs={String.valueOf(pkID)};
return db.delete(DATABASE_TABLE_4,"pk_pkID == ?",whereArgs);
adapter.notifyDataSetChanged();
db.close();
}
catch(Exception e)
{
}
} });
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
} });
alertDialog.setMessage(message);
alertDialog.show();
}
Does it remove it from your list adapter?
If not that would be the reason the notifyDataSetChanged()
won't do you much good.
Actually looking at your code again i can only find that you're removing it from your database and not the adapter itself.
edit (to answer comment):
Well that's hard to do without your ListView_CustomAdapter
class.
The problem is, in this adapter there's a data set (the one you put in the constructor (listitemDisplay
)) which needs to be updated as well. Only then the notifyDataSetChanged()
will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With