[英]Android DialogFragment
I use a class which extends DialogFragment, and in this class I use an AsyncTask
for loading the content of my AlertDialog
. 我使用了一个扩展DialogFragment的类,并且在该类中,我使用了AsyncTask
来加载AlertDialog
的内容。
My question is: how I can create an AlertDialog
which shows: "Loading..." and then shows the content in my onPostExecute
method? 我的问题是:如何创建显示“正在加载...”的AlertDialog
,然后在onPostExecute
方法中显示内容?
public class MyDialog extends DialogFragment {
public MyDialog(){
super();
}
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
....
return builder.create();
}
public class myTask extends Asynctask<Void, Void, Void> {
@Override
protected Void onPreExecute(){
Show loading... in AlertDialog
}
@Override
protected Void doInBackground(Void... voids) {
Load content...
}
@Override
protected Void onPostExecute(){
Show content in AlertDialog
}
}
For that purpose, my suggestion is that don't use AlertDialog. 为此,我的建议是不要使用AlertDialog。 Use a progress dialog, Here is your complete dialog, 使用进度对话框,这是您的完整对话框,
public class MyDialog extends DialogFragment {
public MyDialog(){
super();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_dialog_id, container, false);
new myTask().execute();
return view;
}
private void afterContentLoad (Object content)
{
//Update your UI with respect of your content :)
}
public class myTask extends Asynctask<Void, Void, Void> {
private ProgressDialog mDialog;
@Override
protected Void onPreExecute(){
try{
mDialog = ProgressDialog.show(getActivity(), "", "Loading...!");
}
catch(Exception e)
{
//BadTokenException
e.printStackTrace();
}
}
@Override
protected Void doInBackground(Void... voids) {
Load content...
}
@Override
protected Void onPostExecute(){
if (mDialog != null && mDialog.isShowing()){
mDialog.dismiss();
}
//Put your code after content loaded
afterContentLoad(Object);
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.