我希望在一个DialogFragment中显示来自互联网的信息,我有些疑惑,吼叫:
1 - 为此,我在DialogFragment中实现了AsynkTask,但我的活动同时运行另一个AsynkTask,因此dialogfragment需要等待来自活动的AsynkTask。可以在活动之前从DialogFragment执行AsynkTask吗?
2 - 我尝试在DialogFragment中使用一个DialogProgress,但它在活动中显示,如何在DialogFragment中显示?
3 - 可以在没有AsyncTask的情况下在DialogFragment中查询来自互联网的信息吗?
public class DialogFotoFragment extends DialogFragment {
private String latitude;
private String longitude;
private Handler hndl = new Handler();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_foto_ponto, container);
// Verifica se a latitude e lngitude foram informados
if (latitude == null || latitude.trim().length() == 0) {
Toast.makeText(this.getActivity(),
"Não foi possível apresentar a foto", Toast.LENGTH_LONG)
.show();
this.dismiss();
}
if (longitude == null || longitude.trim().length() == 0) {
Toast.makeText(this.getActivity(),
"Não foi possível apresentar a foto", Toast.LENGTH_LONG)
.show();
this.dismiss();
}
ImageView img = (ImageView) view.findViewById(R.id.fotoPonto);
Button btn = (Button) view.findViewById(R.id.btnOKDialogFoto);
btn.setOnClickListener(buttonOk);
ProgressDialog pDialog = ProgressDialog.show(getActivity(), "",
"Wait...", true);
// Apresenta tela de aguarde
ConsultaFotoBackGround consultaFoto = new ConsultaFotoBackGround();
Object[] objs = new Object[3];
objs[0] = img;
objs[1] = this;
objs[2] = pDialog;
consultaFoto.execute(objs);
return view;
}
private OnClickListener buttonOk = new OnClickListener() {
@Override
public void onClick(View arg0) {
DialogFotoFragment.this.dismiss();
}
};
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
private class ConsultaFotoBackGround extends
AsyncTask<Object, Void, Object[]> {
private Handler hndl = new Handler();
Drawable fotoInternet;
ImageView img;
ProgressDialog pDialog;
@Override
protected Object[] doInBackground(Object... objs) {
img = (ImageView) objs[0];
DialogFotoFragment classe = (DialogFotoFragment) objs[1];
pDialog = (ProgressDialog)objs[2];
try {
fotoInternet = Utilities.LoadImagePonto(latitude,
longitude);
showFoto();
} catch (Exception e) {
Toast.makeText(classe.getActivity(),
"Não foi possível apresentar a foto", Toast.LENGTH_LONG)
.show();
classe.dismiss();
}
return objs;
}
@Override
protected void onPostExecute(Object[] result) {
}
protected void showFoto() {
hndl.post(new Runnable() {
@Override
public void run() {
img.setImageDrawable(fotoInternet);
pDialog.dismiss();
}
});
}
}
}
答案 0 :(得分:1)
使用AsyncTask
无法获得所需信息,然后在其onPostExecute
中,您可以使用刚刚获得的数据创建DialogFragment
。
按照官方文档了解如何正确创建对话框(即使使用自定义布局):http://developer.android.com/guide/topics/ui/dialogs.html