首页 > 解决方案 > 如何将异步任务用于不同的值。我将在描述中简要解释我的问题

问题描述

在此处输入图像描述我正在使用 volley 从服务器检索数据并显示在列表视图中。所以我将从服务器获取客户的地址,我需要计算使用该应用程序的人的当前位置与我从服务器获得的地址之间的距离。我将获得不同的客户地址,我需要计算他们所有的距离并与客户名称一起在列表视图中单独显示。所以我使用以下代码。我得到的所有客户的距离与图像中显示的相同。有地方,thane,delhi,kalyan。我需要计算距离从当前位置到那些地方。我不明白问题是什么。使用 AyncTask 计算距离的代码。此代码在片段内。

public class getLocationFromNameAsync extends 
  AsyncTask<String  ,String,Double> {
    Context context;
    onTaskCompleted completed;

    public getLocationFromNameAsync(Context context) {
        this.context = context;
        this.completed = (onTaskCompleted) context;
    }

    @Override
    protected Double doInBackground(String... strings) {
        Geocoder coder = new Geocoder(getActivity());

        List<Address> address1=null;
        List<Address> addresses = null;

        try {
            address1 = coder.getFromLocationName(district, 5);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (address1 == null) {
            Toast.makeText(getActivity(), "Fetching Location,Please wait", Toast.LENGTH_SHORT).show();
            progressBar.setVisibility(View.INVISIBLE);
        }
        final Address location11 = address1.get(0);
        location11.getLatitude();
        location11.getLongitude();
        if (checkPermission()) {
            LocationManager locationManager=(LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
            Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            Location location2 = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

            if (location != null) {
                latti = location.getLatitude();
                longi = location.getLongitude();
            } else if (location1 != null) {
                latti = location1.getLatitude();
                longi = location1.getLongitude();
            } else if (location2 != null) {
                latti = location2.getLatitude();
                longi = location2.getLongitude();
            }
            coder = new Geocoder(getActivity(), Locale.getDefault());

            try {
                addresses = coder.getFromLocation(latti, longi, 1);
                if (addresses != null && addresses.size() > 0) {
                    String address = addresses.get(0).getAddressLine(0);
                    area = addresses.get(0).getLocality();
                    String city = addresses.get(0).getAdminArea();
                    String county = addresses.get(0).getCountryName();
                    String postal_code = addresses.get(0).getPostalCode();
                    fullAddress=address+","+area+","+city+","+county+","+postal_code;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        LatLng source = new LatLng(latti, longi);

        LatLng destination = new LatLng(location11.getLatitude(), location11.getLongitude());
        double dis= SphericalUtil.computeDistanceBetween(source,destination);
        dis/=1000;
        return dis;
    }

    @Override
    protected void onPostExecute(Double aDouble) {
       completed.onTaskComplete(aDouble);
    }
}

我的界面代码。

public interface onTaskCompleted {
public void onTaskComplete(Double result);
}

navigationDrawerActivity 代码。在这个活动中,我得到了结果。

public class navigationDrawer extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener ,onTaskCompleted{
  static double distance;
 @Override
public void onTaskComplete(Double result) {
    distance=result;

    Toast.makeText(getApplicationContext(),"result is "+result,Toast.LENGTH_SHORT).show();
}

}

从 API 获取数据并在 listView 中设置数据

StringRequest stringRequest = new StringRequest(Request.Method.POST, getTaskUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    arrayList.clear();
                    try {
                        JSONObject jo = new JSONObject(response);
                        JSONArray ja = jo.getJSONArray("dataObj");
                        int length=ja.length();

                        for(int i=0; i<length; i++){
                            JSONObject temp = ja.getJSONObject(i);
                           nameC= temp.getString("name");
                           checkId=temp.getString("clientEid");
                           clientName=temp.getString("client");
                           candidateFatherName=temp.getString("fatherName");
                           clientProcess=temp.getString("otherId");
                            dueDate=temp.getString("deadline");
                            JSONArray jsaDate = temp.getJSONArray("updateDetails");
                            for(int k=0; k<jsaDate.length(); k++){
                                JSONObject jo1 = jsaDate.getJSONObject(k);
                                sentDate = jo1.getString("date");
                            }

                            JSONArray ja1 = temp.getJSONArray("address");
                            for(int j=0; j<ja1.length(); j++){
                                JSONObject jo1 = ja1.getJSONObject(j);
                                landmark=jo1.getString("landmark");
                                district= jo1.getString("district");
                                userAddrss=jo1.getString("full");

                                Geocoder coder = new Geocoder(getActivity());
                                       new getLocationFromNameAsync(getContext()).execute(district);

                                arrayList.add(new DataModel(nameC,district,landmark,checkId,Math.floor((navigationDrawer.distance*100)/100),clientName,candidateFatherName,clientProcess,sentDate,dueDate));
                            }
                        }

标签: androidperformancelistviewandroid-asynctaskandroid-volley

解决方案


@MarconVasconcelos 说您需要迭代所有列表项,并为每个项应用收到的响应


推荐阅读