Android - Listview 删除项目和刷新 [英] Android - Listview delete item and Refresh

查看:70
本文介绍了Android - Listview 删除项目和刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用删除功能实现 ListView,以从列表视图中删除项目.从数据库中删除项目后,我成功删除但未能刷新列表视图.

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.

实际上,单击列表项,我正在显示删除"和取消"操作的警报框,单击删除"时,应从数据库以及列表视图中删除项目,并且应刷新列表视图.我也使用了 notifyDataSetChanged() 方法.

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);
     }
});

以及 Show_Alert_box 的代码:

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();
     }

推荐答案

使用 Intent 再次调用该 Activity

Call that Activity once again Using Intent

这篇关于Android - Listview 删除项目和刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆