gpt4 book ai didi

android - 如何显示进度条?

转载 作者:行者123 更新时间:2023-11-30 03:55:09 25 4
gpt4 key购买 nike

我正在构建一个应用程序,它将使用 GeoCoder 根据用户输入的街道地址。附上我制作的一段代码:

Geocoder gc = new Geocoder(getBaseContext(), Locale.getDefault());

List<Address> addresses = null;
StringBuilder sb = new StringBuilder();
String destination = edittext_destination.getText().toString();

try {
addresses = gc.getFromLocationName(destination, 10);
} catch (Exception e){
Toast.makeText(getBaseContext(), "Address not found", Toast.LENGTH_SHORT).show();
}

上面的代码可以运行,但需要一些时间才能返回结果。在等待结果时,我想显示进度微调器。我知道它应该使用 Thread 但我不知道如何开始。我希望任何人都可以提供帮助。

谢谢

最佳答案

您可以使用 AsyncTask 来做到这一点:

    final String destination = edittext_destination.getText().toString();
new AsyncTask<String, Void, List<Address>>() {
private Dialog loadingDialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(TestActivity.this, "Please wait", "Loading addresses...");
}

@Override
protected List<Address> doInBackground(String... params) {
String destination = params[0];
try {
Geocoder gc = new Geocoder(getBaseContext(),
Locale.getDefault());
return gc.getFromLocationName(destination, 10);
} catch (Exception e) {
return null;
}
}

@Override
protected void onPostExecute(List<Address> addresses) {
loadingDialog.dismiss();

if (addresses == null) {
Toast.makeText(getBaseContext(), "Geocoding error",
Toast.LENGTH_SHORT).show();
} else if (addresses.size() == 0) {
Toast.makeText(getBaseContext(), "Address not found",
Toast.LENGTH_SHORT).show();
} else {
// Do UI stuff with your addresses
Toast.makeText(getBaseContext(), "Addresses found: " + addresses.size(), Toast.LENGTH_SHORT).show();
}
}
}.execute(destination);

关于android - 如何显示进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13470542/

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