简体   繁体   English

如何通过单击 android 中的按钮重新加载 listView?

[英]How to reload the listView on click of a button in android?

I have created a view in which i have 1 list view and 2 buttons.我创建了一个视图,其中有 1 个列表视图和 2 个按钮。 The first button is for deleting the selected list view and the 2nd button is for deleting the deleted the entire data from the table.第一个按钮用于删除选定的列表视图,第二个按钮用于从表中删除已删除的整个数据。 I want that if I delete the entire data from the table the list view should get refreshed an it should get reloaded.我希望如果我从表中删除整个数据,列表视图应该被刷新并且它应该被重新加载。 Please help me out in achieving this.Thanks in advance.请帮助我实现这一目标。在此先感谢。

use should use notifyDataSetChanged () method to refresh your listview...使用应该使用 notifyDataSetChanged() 方法来刷新你的列表视图...

Call notifyDataSetChanged() on your Adapter在适配器上调用 notifyDataSetChanged()

see this link........ http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged%28%29看到这个链接........ http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged%28%29

you can follow the following way您可以按照以下方式

1) create a handler to add or remove item to your listview 1)创建一个处理程序来添加或删除项目到您的列表视图

static final int UPDATE_LIST =1;
static final int CLEAR_LIST = 2;

    private Handler photoGridHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case UPDATE_LIST:

                int index = msg.arg1;

                Bitmap bitMapImage = (Bitmap) msg.obj;
                mylist.add(bitMapImage);
myAdapter.notifyDataSetChanged();
                break;

            case CLEAR_LIST:
                                 int index = msg.arg1;
                mylist.remove(index );
                myAdapter.notifyDataSetChanged();
break;

            }
        };
    };

2) call this handler for different type of operation either you want to add data or remove data from the list. 2) 为不同类型的操作调用此处理程序,您想添加数据或从列表中删除数据。 So handler should be called inside your onClickListener() .所以应该在你的onClickListener()中调用处理程序。 See the below how to call handler请参阅下面如何调用处理程序

        to remove element from the list use the following

Message msg = new Message();
        msg.what = CLEAR_LIST;
        msg.arg1 = index;

        photoGridHandler.sendMessage(msg);

to add elements to the list use the folowing将元素添加到列表中使用以下

Message msg = new Message();
        msg.what = UPDATE_LIST;
        msg.arg1 = index;
        msg.obj = image;
        photoGridHandler.sendMessage(msg);

Thanks Deepak谢谢迪帕克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM