开发者

How to handle listView item changes in managed option menu dialogs?

开发者 https://www.devze.com 2023-03-10 08:07 出处:网络
I create a dialog in my activities onCreateDialog method like this ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

I create a dialog in my activities onCreateDialog method like this

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.select_dialog_item, android.R.id.text1, items);
dialog = new AlertDialog.Builder(this).setTitle(R.string.dialogTitle).setAdapter(adapter,
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                // do something
            }
        }).create();

The items I want to show in the dialog are in a simple ArrayList

private List<String> items = new ArrayList<String>();

The dialog is now managed (saved and restored) by my activity - as I understand. Thus it is not recreated every time the user presses the menu button to open the dialog.

According to some user selection on the activity - the item list of the dialog needs to be changed. I thought this would be no great problem but after changing the content of the list I run into the following exception:

06-03 10:55:29.263: ERROR/AndroidRuntime(276): java.lang.IllegalStateException: 
The content of the adapter has changed but ListView did not receive a notification. 
Make sure the content of your adapter is not modified from 开发者_StackOverflowa background thread, 
but only from the UI thread. [in ListView(16908785, class com.android.internal.app.AlertController$RecycleListView) with Adapter(class android.widget.ArrayAdapter)]

I debugged the Thread.currentThread().getId() and found it always to be "1" (creation of the dialog and also changing the items list).

How can I handle the item list changes to be noticed by my dialog? Or should I avoid using a "managed" dialog and create it from scratch every time the user opens it?

How can/should I make things work?

Thanks for any suggestions!


Make sure that ArrayAdapter.setNotifyOnChange() is enabled, or properly call Adapter.notifyDataSetChanged() after data change.


Reading you, I get a feeling like you're not properly saving/restoring something with onSaveInstanceState() and onRestoreInstanceState().

When an activity is paused and resumed, the view hierarchy of managed dialogs is automatically saved and restored. That obviously includes the ListView.

But if the adapter state and/or data is not restored to its previous state, then the ListView may detect an unconsistency between it's own (restored) state and the attached adapter. For instance, I can imagine problems if the number of items in the adapter changes after pause/resume.

This could be coherent with the error message you get, something has changed in the adapter but the ListView did not get noticed, except that this may be caused by inconsistent state saving/restoring as I explain, instead of a threading issue.

Make sure that you properly save and restore the state of the adapter.

0

精彩评论

暂无评论...
验证码 换一张
取 消