简体   繁体   English

如何在onclick中刷新listadapter

[英]How to refresh a listadapter from within an onclick

I have an Android app that displays a list using a ListView, and there's an action bar button to clear said list. 我有一个Android应用程序,该应用程序使用ListView显示列表,并且有一个操作栏按钮可清除所述列表。 I decided to add a confirmation dialog so people don't accidentally delete all their entries, and I'm running into problems. 我决定添加一个确认对话框,以便人们不会意外删除所有条目,而我遇到了问题。 If I use setListAdapter inside the onclick for the "yes" button within the dialog, it won't compile. 如果我在对话框中的“是”按钮的onclick内使用setListAdapter,它将无法编译。 If I use it outside that onclick, it'll work but not refresh the list until the user backs out of the activity and goes back into it, which for obvious reasons is not appropriate. 如果我在onclick之外使用它,它将起作用,但直到用户退出活动并返回活动列表后,它才刷新列表,这显然是不合适的。 Here's my method that gets called when the "clear list" action bar button is pressed, which contains the relevant onclick for the internal buttons. 这是我的方法,当按下“清除列表”操作栏按钮时会被调用,其中包含内部按钮的相关onclick。 I have a feeling I shouldn't be using "this" in setListAdapter since with the dialog, this no longer corresponds to the listview activity I think? 我有种感觉,我不应该在setListAdapter中使用“ this”,因为有了对话框,它不再对应于我认为的listview活动? But I'm not sure what to put instead. 但是我不确定该放些什么。

public void clearTrigger(MenuItem item) {
    //Set up a dialog with two buttons to verify that the user really wants to delete      
everything
    confirm = new Dialog(display.this);
    confirm.setContentView(R.layout.conf);
    confirm.setTitle("Confirm deletion");
    yes = (Button)confirm.findViewById(R.id.yes);

    //If the user says yes, then delete everything
    yes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    //Remove everything from Hours.
                    Hours.clear();
                    String tempH = " ";
                    String tempW = " ";

                    //Then save it again in it's new, empty state so that it doesn't reappear the next time the app is run.
                    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                    SharedPreferences.Editor edit = prefs.edit();
                    edit.putString("SAVEDATA", TextUtils.join(",", Hours));
                    edit.remove("totalh");
                    edit.remove("totalw");
                    edit.commit();
                    //And finally... refresh the list view - doesn't work
                    setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_list, R.id.listText, Hours));
                    confirm.dismiss();
                }

            });
            confirm.show();
}

The first argument of ArrayAdapter constructor is a Context , so you need to pass the Activity to it, something like new ArrayAdapter<String>(MyActivity.this, ...) . ArrayAdapter构造函数的第一个参数是Context ,因此您需要将Activity传递给它,例如new ArrayAdapter<String>(MyActivity.this, ...) Right now you're passing it your instance of OnClickListener which is why it's giving compiler error. 现在,您正在向它传递您的OnClickListener实例,这就是为什么它会导致编译器错误。

But the best way to update a ListView is to make changes on the ArrayAdapter itself using methods like adapter.add and adapter.remove , and then call adapter.notifyDataSetChanged() . 但是更新ListView的最好方法是使用adapter.addadapter.remove类的方法对ArrayAdapter本身进行更改,然后调用adapter.notifyDataSetChanged() In your case, you would call adapter.clear() . 在您的情况下,您将调用adapter.clear()

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

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