Android 如何对不同的值使用异步任务。我将在说明中简要解释我的问题

Android 如何对不同的值使用异步任务。我将在说明中简要解释我的问题,android,performance,listview,android-asynctask,android-volley,Android,Performance,Listview,Android Asynctask,Android Volley,我正在使用截击从服务器获取检索到的数据,并显示在列表视图中。因此,我将从服务器获取客户端地址,我需要计算使用该应用程序的人的当前位置与我从服务器获取的地址之间的距离。我将获取不同的客户端地址,我需要计算它们的所有距离,并在listview中与客户端名称一起单独显示。因此,我使用以下代码。我得到的所有客户的距离与图中显示的相同。有一些地方,塔恩、德里、卡扬。我需要计算从当前位置到这些地方的距离。我不明白问题出在哪里。使用AyncTask计算距离的代码。此代码位于片段内部 public class

我正在使用截击从服务器获取检索到的数据,并显示在列表视图中。因此,我将从服务器获取客户端地址,我需要计算使用该应用程序的人的当前位置与我从服务器获取的地址之间的距离。我将获取不同的客户端地址,我需要计算它们的所有距离,并在listview中与客户端名称一起单独显示。因此,我使用以下代码。我得到的所有客户的距离与图中显示的相同。有一些地方,塔恩、德里、卡扬。我需要计算从当前位置到这些地方的距离。我不明白问题出在哪里。使用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);
    }
}
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));
                            }
                        }
StringRequest-StringRequest=newstringrequest(Request.Method.POST,getTaskUrl,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
arrayList.clear();
试一试{
JSONObject jo=新的JSONObject(响应);
JSONArray ja=jo.getJSONArray(“dataObj”);
int length=ja.length();

对于(int i=0;i@MarconVasconcelos是说您需要迭代所有列表项,并为每个列表项应用收到的响应

您错过了一个循环,通过所有项将其设置为模型,而不是视图,您将始终使用最新的值calculated覆盖。我没有得到您的答案。请详细说明
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));
                            }
                        }