dhsmith
dhsmith

Reputation: 250

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

Upvotes: 0

Views: 113

Answers (1)

ashishduh
ashishduh

Reputation: 6699

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, ...). Right now you're passing it your instance of OnClickListener which is why it's giving compiler error.

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(). In your case, you would call adapter.clear().

Upvotes: 1

Related Questions