Android的AlertDialog与动态地在每次请求更改文本 [英] Android AlertDialog with dynamically changing text on every request

查看:278
本文介绍了Android的AlertDialog与动态地在每次请求更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示一个AlertDialog一个选项,可能会改变在每次请求。因此,例如在同一时间我想显示选择添加到通讯录,而其他时间应该是从通讯录中删除。

我的code并在第一次工作,但是Android的似乎缓存AlertDialog使onCreateDialog不执行下一次。因此,选择不改变了。我能prevent这种缓存,或者是有改变选择的另一种方式吗?

我的工作与SDK 1.5,但使用1.1​​。

  @覆盖
受保护的对话框onCreateDialog(最终诠释的id){
    ...
    字符串add_remove_contact = res.getString(R.string.profile_add_to_contacts);
    如果(user.getContacts()。包括(配置文件ID)){
        add_remove_contact = res.getString(R.string.profile_remove_from_contacts);
        // TODO:此字符串不改变,当接触状态发生变化
    }
    最后的CharSequence []项目= {res.getString(R.string.view_profile)
                                  res.getString(R.string.profile_send_message)
                                  add_remove_contact};
    AlertDialog.Builder建设者=新AlertDialog.Builder(本);
    ...
    返回builder.create();
}
 

解决方案

您也可以使用 removeDialog(INT)活动的功能。当被驳回的对话,活动基本保存对话框(因为性能原因我会想象)的状态。呼叫 removeDialog(INT)对话框上强制的活动卸载的对话框中所有引用,并从屏幕上驳斥它,如果它被显示。

创建对话框
<一href="http://developer.android.com/reference/android/app/Activity.html#removeDialog%28int%29">Activity#removeDialog(int)

I want to show an AlertDialog with one option that might change on every request. So for example at one time I want to show the option "add to contacts" while another time it should be "remove from contacts".

My code does work on the first time, however Android seems to cache the AlertDialog so that onCreateDialog is not executed next time. Therefore the option doesnt change anymore. Can I prevent this caching, or is there just another way of changing the option?

I am working with SDK 1.5 but using 1.1.

@Override
protected Dialog onCreateDialog(final int id) {
    ...
    String add_remove_contact = res.getString(R.string.profile_add_to_contacts);
    if (user.getContacts().contains(profileID)) {
        add_remove_contact = res.getString(R.string.profile_remove_from_contacts);
        // TODO: this string is not changed when contact status changes 
    }
    final CharSequence[] items = {res.getString(R.string.view_profile),
                                  res.getString(R.string.profile_send_message),
                                  add_remove_contact};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    ...
    return builder.create();
}

解决方案

You can also use the removeDialog(int) function of the Activity. When a dialog is dismissed, the Activity basically stores the state of the dialog (for performance reasons I would imagine). Calling removeDialog(int) on the dialog forces the activity to unload all references for the dialog and dismisses it from the screen if it's being shown.

Creating Dialogs
Activity#removeDialog(int)

这篇关于Android的AlertDialog与动态地在每次请求更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆