【问题标题】:Android AsyncTask passing valuesAndroid AsyncTask 传递值
【发布时间】:2015-03-16 15:41:29
【问题描述】:

我想获取用户在 DialogFragment 中的当前位置并将所有数据传递给 SQLite 数据库。在我的 DoInBackground 中,我得到了运行良好的当前位置

我想设置一个 long 和 lat 的 TextView,如何将值传递给我的 AysncTask 并返回到我的对话框

 protected getLocation doInBackground(String... params) {

}

    protected void onPostExecute(getLocation result) {

    }
    }

我想在外部类中传递值,以便我可以将数据放入 sql 中

public Dialog onCreateDialog(Bundle savedInstanceState){


    final AlertDialog.Builder build = new AlertDialog.Builder(getActivity());

    text = (TextView) view.findViewById(R.id.insert_long);
    text2 = (TextView) view.findViewById(R.id.insert_lat);
    build.setView(view);

    db = new DBHelper(getActivity());
    db.open();
    build.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {


            }
        }
    });

【问题讨论】:

  • 我不是 100% 确定您的要求。是否要从 AsyncTask 返回位置?如果是这样,您可以将 AsyncTask 的返回类型从 String... params 更改为 Location... params
  • 我想返回 long 和 lat 以便我可以将它们设置为 TextView 并通过 onClick 将它们存储到 SQLite 中。 db.insert() 是我的 DBhelper 中的一个方法
  • 你能发布你如何调用你的 asynctask 的代码吗?
  • 我已经更新了我的代码
  • @Mark - 这不是完整的代码,因为我在您的LocationTask execute() 上找不到行,control 是什么?

标签: android android-asynctask android-dialogfragment android-location


【解决方案1】:

异步任务使用的三种类型如下:

  1. Params,执行时发送给任务的参数类型。
  2. 进度,进度单位在期间发布的类型 后台计算。
  3. Result,后台计算结果的类型。

所以你需要传递 Double[] 或你的自定义结构包装 lat,lon 作为结果。

 private class MyTask extends AsyncTask<Void, Void, Double[]>

并实现onPostExecute(Double[] result)方法。

【讨论】:

    【解决方案2】:

    在声明类的时候可以指定方法doInBackground(...)的返回类型。 像这样:

    private class LocationReceiver extends AsyncTask<String, Void, double[]> {
    protected String doInBackground(String... params) {
        try {
            LocationManager loca = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            return new double[] {longitude, latitude};
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    
    protected void onPostExecute(double[] resultArray) {
       // Do something with the returned location
       double lon = resultArray[0];
       double lat = resultArray[1];
    }
    

    }

    【讨论】:

    • 我只想简单地获取用户的当前位置,因为它减慢了我的主线程,所以现在我想将 Long 和 lat 传递回我的对话框
    • 我更新了我的答案。您还可以创建一个仅包含纬度和经度的简单 POJO 类。
    【解决方案3】:

    使用纬度和经度变量创建自定义类:

    class LatLng {
       public final  double latitude;
       public final  double longitude;
    
       LatLng(double latitude, double longitude) {
           this.latitude = latitude;
           this.longitude = longitude;
       }
    }
    

    现在从doInBackground 方法返回LatLng 对象,以便在onPostExecute 方法中获取两个值:

    protected LatLng doInBackground(String... params) {
            ...
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
      return new LatLng(location.getLatitude(),location.getLongitude());
    
    }
    

    并将LocationTask 类更改为:

    private static class LocationTask extends AsyncTask<Location,
                                                 Void, getLocation> {
    
        ....
        @Override
        protected getLocation doInBackground(String... params) {  
         // your code here
    
        }
        @Override
        protected void onPostExecute(getLocation result) {
    
            text = (TextView) context.findViewById(R.id.textName);
            text.setText(result.latitude +"--"+result.longitude);
        }
      }
    

    【讨论】:

    • 你会用我的代码写吗,我不太明白
    • @user370305:两者都是正确的,要么使用自定义类,要么使用默认的 LatLng 类,而不是返回 Location 对象
    • @Mark:那有什么问题?
    • @ρяσѕρєяK 在 DoInBackground 获得 long 和 lat 后,我​​希望 postexecute 将我的 TextView 设置为 Long 和 Lat,以及如何在我的 db.insert() 方法中调用 long 和 lat 跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 2016-05-14
    相关资源
    最近更新 更多