问题描述
有人可以指出一个自定义对话框的工作示例,它将ArrayAdapter作为输入并显示可选列表。我已经尝试使用AlertDialog Builder这样...
final ArrayAdapter< MyObject> myAdapter = getMyobjects();
final AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(Pick a 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();
返回警报;
我的问题是我的对话框没有更新,然后我调用
@Override
protected void onPrepareDialog(final int id,final Dialog dialog){
switch(id){
case DIALOG_GET_AVAIL_DESTS:
((AlertDialog)对话框).getListView()。setAdapter(getDestinations());
break;
}
}
但是,onClick侦听器侦听初始的项目集
由于您正在使用 onPrepareDialog(int id,Dialog dialog) $ c>,我猜你最初在
onCreateDialog(int id)
中设置对话框。
这样做会导致系统保存您最初创建的对话框。为了实现所需的功能,当对话框被关闭时,通过调用 android.app.Activity.removeDialog(int id)
来告诉系统丢弃它。
任何后续的调用都将通过 onCreateDialog(int id)
方法重新生成对话框,从而导致一组项目被更新。
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 = getMyobjects();
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...
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.
这篇关于Android自定义列表对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!