Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何显示进度微调器?_Android_Multithreading_Progress - Fatal编程技术网

Android 如何显示进度微调器?

Android 如何显示进度微调器?,android,multithreading,progress,Android,Multithreading,Progress,我正在构建一个应用程序,该应用程序将使用GeoCoder根据用户的输入进行街道地址设置。下面是我写的一段代码: Geocoder gc = new Geocoder(getBaseContext(), Locale.getDefault()); List<Address> addresses = null; StringBuilder sb = new StringBuilder(); String destination = edittext_destination.getTex

我正在构建一个应用程序,该应用程序将使用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();
}
Geocoder gc=new Geocoder(getBaseContext(),Locale.getDefault());
列表地址=空;
StringBuilder sb=新的StringBuilder();
String destination=edittext_destination.getText().toString();
试一试{
地址=gc.getFromLocationName(目的地,10);
}捕获(例外e){
Toast.makeText(getBaseContext(),“找不到地址”,Toast.LENGTH_SHORT.show();
}
上面的代码正在运行,但返回结果需要一些时间。在等待结果时,我想显示进度微调器。我知道它应该使用线程,但我不知道如何开始。我真希望任何人都能帮忙


谢谢你

你可以通过一个
异步任务

    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);
final String destination=edittext_destination.getText().toString();
新建异步任务(){
私有对话框加载对话框;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
loadingDialog=ProgressDialog.show(TestActivity.this,“请稍候”,“加载地址…”);
}
@凌驾
受保护列表doInBackground(字符串…参数){
字符串目的地=参数[0];
试一试{
Geocoder gc=新的地理编码器(getBaseContext(),
Locale.getDefault());
返回gc.getFromLocationName(目的地,10);
}捕获(例外e){
返回null;
}
}
@凌驾
受保护的void onPostExecute(列表地址){
loadingDialog.disclose();
如果(地址==null){
Toast.makeText(getBaseContext(),“地理编码错误”,
吐司。长度(短)。show();
}else if(addresses.size()==0){
Toast.makeText(getBaseContext(),“找不到地址”,
吐司。长度(短)。show();
}否则{
//用你的地址做用户界面
Toast.makeText(getBaseContext(),“找到的地址:”+Addresses.size(),Toast.LENGTH\u SHORT.show();
}
}
}.执行(目的地);

您可以通过一个
异步任务来完成:

    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);
final String destination=edittext_destination.getText().toString();
新建异步任务(){
私有对话框加载对话框;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
loadingDialog=ProgressDialog.show(TestActivity.this,“请稍候”,“加载地址…”);
}
@凌驾
受保护列表doInBackground(字符串…参数){
字符串目的地=参数[0];
试一试{
Geocoder gc=新的地理编码器(getBaseContext(),
Locale.getDefault());
返回gc.getFromLocationName(目的地,10);
}捕获(例外e){
返回null;
}
}
@凌驾
受保护的void onPostExecute(列表地址){
loadingDialog.disclose();
如果(地址==null){
Toast.makeText(getBaseContext(),“地理编码错误”,
吐司。长度(短)。show();
}else if(addresses.size()==0){
Toast.makeText(getBaseContext(),“找不到地址”,
吐司。长度(短)。show();
}否则{
//用你的地址做用户界面
Toast.makeText(getBaseContext(),“找到的地址:”+Addresses.size(),Toast.LENGTH\u SHORT.show();
}
}
}.执行(目的地);
使所有这些异步内容变得非常简单。您可以添加微调器和进度条,只需比异步调用多一点,如下所示:

设置:

ProgressDialog dialog = new ProgressDialog(this);

dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.setInverseBackgroundForced(false);
dialog.setCanceledOnTouchOutside(true);
dialog.setTitle("Sending...");

String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";                
您可以使用异步调用:

aq.progress(dialog).ajax(url, JSONObject.class, this, "jsonCb");
看看!出色的响应时间和Peter Liu的帮助。这个项目有利于开发人员

使所有这些异步内容变得超级简单。您可以添加微调器和进度条,只需比异步调用多一点,如下所示:

设置:

ProgressDialog dialog = new ProgressDialog(this);

dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.setInverseBackgroundForced(false);
dialog.setCanceledOnTouchOutside(true);
dialog.setTitle("Sending...");

String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";                
您可以使用异步调用:

aq.progress(dialog).ajax(url, JSONObject.class, this, "jsonCb");

看看!出色的响应时间和Peter Liu的帮助。这个项目有利于开发人员

看一看这篇文章:看一看这篇文章:谢谢你的快速反应提琴手。我会试试看。我正要发布一个答案,但却被其他问题缠住了。如果添加onPreExecute()方法,在其中显示微调器并将其隐藏在onPostExecute()方法中,这个答案就可以了。但不幸的是,它还没有工作。我很抱歉地告诉你们,代码没有工作。我不知道怎么了。我应该在这里给出我的全部代码吗?我忘记了
.execute(目的地)在我的代码中…因此任务从未执行过。用更新的代码再试一次谢谢你的快速响应提琴手。我会试试看。我正要发布一个答案,但却被其他问题缠住了。如果添加onPreExecute()方法,在其中显示微调器并将其隐藏在onPostExecute()方法中,这个答案就可以了。但不幸的是,它还没有工作。我很抱歉地告诉你们,代码没有工作。我不知道怎么了。我应该在这里给出我的全部代码吗?我忘记了
.execute(目的地)在我的代码中…因此任务从未执行过。请使用更新的代码重试