【问题标题】:view on dialog doesn't change when it should对话框视图不会改变
【发布时间】:2015-05-23 18:07:41
【问题描述】:

我有一个ListView,当单击一个项目时,它会创建新的 PlaySongAlertDialog 对象并将参数传递给它。现在下面代码的问题是,只有在第一次它实际上改变了artistCheckBox 的文本,当我点击关闭然后在另一个ListView 行上它显示artistCheckBox 上的相同文本。 我找不到帖子,但我记得有人说我应该覆盖onPrepareDialog,但我在AlertDialog.Builder 类上找不到这种方法。

public class PlaySongAlertDialog extends AlertDialog.Builder {
    public PlaySongAlertDialog(final Context context, final String songName, final Intent service) {
        super(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        View dialoglayout = inflater.inflate(R.layout.download_song_alert_dialog_check_box, null);
        final CheckBox artistCheckBox = (CheckBox) dialoglayout.findViewById(R.id.artist_checkbox);
        final CheckBox albumCheckBox = (CheckBox) dialoglayout.findViewById(R.id.album_checkbox);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    artistCheckBox.setText("Add artist name (" + getSomethingFromTheWeb.getArtist(songName) +")");
                } catch(Exception e){
                    artistCheckBox.setText("Add artist name (can't found)" );
                    artistCheckBox.setClickable(false);
                }

            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        setMessage("Do you want to play: " + songName);
        setView(dialoglayout);
}

这也是我创建新对话框的方式(此代码在主要活动中)

DownloadSongAlertDialog dialog = new DownloadSongAlertDialog(this, name, serviceIntent);
dialog.show();

有没有办法每次都创建一个真实的对话框,而不是重复使用最后一个对话框,也许清理缓存之类的。

【问题讨论】:

    标签: android android-alertdialog layout-inflater


    【解决方案1】:

    thread.join(); 正在阻塞等待其他线程完成的 UI 线程。这就是您看不到对话框更新的原因。要修复你可以继承AsyncTask,并在AlertDialog 的构造函数中启动它。当onPostExecute 被调用时,你填写View 并显示它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 2012-12-09
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      相关资源
      最近更新 更多