开发者

Android Custom List Dialog

开发者 https://www.devze.com 2023-02-15 00:17 出处:网络
Could someone point out a working example of a custom dialog that takes an ArrayAdapter as input and shows a selectable list.

Could someone point out a working example of a custom dialog that takes an ArrayAdapter as input and shows a selectable list.

I have tried to create a Dialog using an AlertDialog Builder as such...

 final ArrayAdapter<MyObject> myAdapter = get开发者_StackOverflowMyobjects();
            final AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("Pick an item").setAdapter(myAdapter,
                    new android.content.DialogInterface.OnClickListener() {
                        public void onClick(final DialogInterface dialog, final int item) {
                            Toast.makeText(Islands.this, myAdapter.getItem(item).toString(), Toast.LENGTH_SHORT).show();    
                        }
                    });
            final AlertDialog alert = builder.create();
            return alert;

My problem is that my dialog is not updating then i called

    @Override
protected void onPrepareDialog(final int id, final Dialog dialog) {
    switch (id) {
         case DIALOG_GET_AVAIL_DESTS:
         ((AlertDialog) dialog).getListView().setAdapter( getDestinations());
         break;
    }
}

However the onClick listener listens to the initial set of items...


Indeed AlertDialog is implements Facade design pattern with this class behind : http://www.netmite.com/android/mydroid/frameworks/base/core/java/com/android/internal/app/AlertController.java

And the whole code is such a mess... I took 3 hours to try to do that, and I am going to build a dialog from scratch, using android.R.layout as a basis.

Steff


You have to make a call to

invalidateViews()

on your listview - that will cause it to redraw the view with the updates.


Since you are using onPrepareDialog(int id, Dialog dialog), I am guessing you're initially setting up the dialog in onCreateDialog(int id).

Doing so cause the system to save the dialog you initially create. In order to achieve the desired functionality, when the dialog is dismissed, tell the system to discard it by calling android.app.Activity.removeDialog(int id).

Any subsequent invocations will have your dialog regenerated through the onCreateDialog(int id) method, causing the set of items to be updated.

0

精彩评论

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