对话框上的视图在应该的时候不会改变

时间:2015-05-23 18:07:42

标签: android alertdialog layout-inflater

我有一个ListView,当点击一个项目时,它会创建新的PlaySongAlertDialog对象并将params传递给它。现在,以下代码中的问题是,只有在第一次实际更改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();

有没有办法每次创建一个真实对话框,而不是重复使用最后一个,可能是清理缓存或其他东西。

1 个答案:

答案 0 :(得分:0)

thread.join();阻止了UI线程等待其他线程完成。这就是为什么你没有看到对话框更新的原因。要解决此问题,您可以继承AsyncTask,并在AlertDialog的构造函数中启动它。调用onPostExecute后,您需要填写View并显示它。