Android的 - 列表视图中删除项,刷新 [英] Android - Listview delete item and Refresh
问题描述
我想实现的ListView与删除功能从列表视图中删除的项目。我成功了删除,但未能从数据库项目的deletetion之后刷新列表视图。
其实,单击列表项,我展示AlertBox的删除和取消的行动,在点击删除,项目应从数据库中被删除,以及从列表视图和ListView应该被刷新。我也用 notifyDataSetChanged()
方法。
LVIEW =(ListView控件)findViewById(R.id.lview);
适配器=新ListView_CustomAdapter(这一点,listitemDisplay);
lview.setAdapter(适配器);
lview.setOnItemClickListener(新OnItemClickListener()
{
@覆盖
公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长ID)
{
Show_Alert_box(v.getContext(),请选择的操作。,位置);
}
});
和$ C $下Show_Alert_box:
公共无效Show_Alert_box(上下文的背景下,字符串消息,INT位置)
{
最终诠释POS =位置;
最后AlertDialog alertDialog =新AlertDialog.Builder(上下文).create();
alertDialog.setTitle(的getString(R.string.app_name_for_alert_Dialog));
alertDialog.setButton(删除,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
尝试
{
db.open();
的String [] whereArgs = {将String.valueOf(PKID)};
返回db.delete(DATABASE_TABLE_4,pk_pkID ==?,whereArgs);
adapter.notifyDataSetChanged();
db.close();
}
赶上(例外五)
{
}
}});
alertDialog.setButton2(取消,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
alertDialog.dismiss();
}});
alertDialog.setMessage(消息);
alertDialog.show();
}
再次致电该活动使用意向
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();
}
Call that Activity once again Using Intent
这篇关于Android的 - 列表视图中删除项,刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!