我有一个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();
有没有办法每次创建一个真实对话框,而不是重复使用最后一个,可能是清理缓存或其他东西。
答案 0 :(得分:0)
thread.join();
阻止了UI线程等待其他线程完成。这就是为什么你没有看到对话框更新的原因。要解决此问题,您可以继承AsyncTask
,并在AlertDialog
的构造函数中启动它。调用onPostExecute
后,您需要填写View
并显示它。