【问题标题】:Not getting value in TextView在 TextView 中没有获得价值
【发布时间】:2014-08-09 10:56:19
【问题描述】:

我正在编写一个程序,其中我必须显示两个位置之间的计算距离,从当前位置到目标位置,并且我已经编写了一个计算位置之间总距离的方法,现在重点是我在 TextView 中没有获得距离值。

这是我的代码,请查看以下内容:

LocationsActivity.java:

    class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(LocationsActivity.this);
            dialog.setMessage("Loading, please wait");
            dialog.setTitle("Connecting server");
            dialog.show();
            dialog.setCancelable(false);
        }

        @Override
        protected Boolean doInBackground(String... urls) {
            try {

                //------------------>>
                HttpGet httppost = new HttpGet(urls[0]);
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httppost);

                // StatusLine stat = response.getStatusLine();
                int status = response.getStatusLine().getStatusCode();

                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);


                    JSONObject jsono = new JSONObject(data);
                    JSONArray jarray = jsono.getJSONArray("locations");
                    Log.d("jarray", jarray.toString());

                    for (int i = 0; i < jarray.length(); i++) {
                        JSONObject object = jarray.getJSONObject(i);

                        locations = new Locations();

                        locations.setName(object.getString("name"));
                        locations.setLatitude(object.getString("latitude"));
                        locations.setLongitude(object.getString("longitude"));

                        actorsList.add(locations);
                    }
                    return true;
                }

                //------------------>>

            } catch (ParseException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return false;
        }

        protected void onPostExecute(Boolean result) {
            dialog.cancel();
            adapter.notifyDataSetChanged();
            if(result == false)
                Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
        }


        public void distanceBetweenTwoLocations() {
        Location currentLocation = new Location("");
        currentLocation.setLatitude(latitude);
        currentLocation.setLongitude(longitude);


        for (int i = 0; i < actorsList.size(); i++) {
            Location destinationLocation = new Location(" ");
            destinationLocation.setLatitude(Double.valueOf(actorsList.get(i).getLatitude()));
            destinationLocation.setLongitude(Double.valueOf(actorsList.get(i).getLongitude()));

            double inMeters = currentLocation.distanceTo(destinationLocation);
            double totalDistance = inMeters / 1000;

            locations.setDistance(totalDistance + "");

           }            
       }
    }
}

【问题讨论】:

  • 您的locationList 为空。
  • 不,我正在从 JSON 获取数据
  • 但是您没有将其添加到列表中。
  • 您在locationsList.get(position).getName() 中获得价值了吗??
  • @SilentKiller 我正在获取列表中的值,请参阅 JSON 中的 doInBacground(...),只是没有获取距离值'

标签: android android-listview gps android-location


【解决方案1】:

问题在于您的 distanceBetweenTwoLocations() 方法。

当您从 locationsList 检索数据时,您正在使用 doInBackground 方法在 actorsList 中添加位置数据

您的 getView 方法仍然存在同样的错误。您在 Locations 列表中添加数据,而在 getView 中您正在从 locationsList 获取数据

更新:

在为locations 列表分配距离时,您将actorsList 传递给适配器。

检查以下代码:

在通知适配器adapter.notifyDataSetChanged();之前,您需要在onPostExecute调用以下方法

public void distanceBetweenTwoLocations() {
    Location currentLocation = new Location("");
    currentLocation.setLatitude(latitude);
    currentLocation.setLongitude(longitude);

    for (int i = 0; i < actorsList.size(); i++) {
        Locations mLocations = (Locations) actorList.get(i);
        Location destinationLocation = new Location(" ");
        destinationLocation.setLatitude(Double.valueOf(actorsList.get(i).getLatitude()));
        destinationLocation.setLongitude(Double.valueOf(actorsList.get(i).getLongitude()));
        double inMeters = currentLocation.distanceTo(destinationLocation);
        double totalDistance = inMeters / 1000;
        mLocations.setDistance(totalDistance + "");
       }            
    }
}

【讨论】:

  • @Moon 你在getView 中获取数据吗?
  • 不,这不是错误,我们可以在 Adapter 类中为我们的 ArrayList 命名
  • 这次我不同意你的看法
  • @Moon come here.
猜你喜欢
  • 1970-01-01
  • 2016-10-20
  • 2021-03-28
  • 2016-01-30
  • 2018-04-08
  • 2018-04-09
  • 2012-06-24
  • 1970-01-01
  • 2019-07-03
相关资源
最近更新 更多