gpt4 book ai didi

android - 在 DialogFragment 中显示来自互联网的信息

转载 作者:行者123 更新时间:2023-11-30 03:21:29 24 4
gpt4 key购买 nike

我想在一个 DialogFragment 中显示来自互联网的信息,我有一些疑问,如下:

1 - 为此,我在 DialogFragment 中实现了一个 AsynkTask,但我的 Activity 同时运行另一个 AsynkTask,因此 dialogfragment 需要等待 Activity 完成 AsynkTask。是否可以在 Activity 之前从 DialogFragment 执行 AsynkTask?

2 - 我尝试在 DialogFragment 中使用一个 DialogProgress,但它在 Activity 中显示,如何在 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();
}
});
}
}

}

最佳答案

你不能使用 AsyncTask 获取你需要的信息然后在它的 onPostExecute 中你可以用你的数据创建一个 DialogFragment刚刚得到。

按照官方文档了解如何正确创建对话框(即使使用自定义布局):http://developer.android.com/guide/topics/ui/dialogs.html

关于android - 在 DialogFragment 中显示来自互联网的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19058944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com