【问题标题】:value of integer in for loop do not changed androidfor循环中的整数值不会改变android
【发布时间】:2013-02-27 08:33:18
【问题描述】:

我正在开发一个使用 Google Places API 的 Android 应用。 一旦我得到所有地点的结果,我想根据算法对其进行排序。 也就是说,如果算法 >= 0,则位置结果只会被放入 Hash Map。 但是现在的问题是,当我运行它时,for循环中的算法结果在循环过程中没有改变。

我的算法是: balance = user_hour-visi-duration。 balance = 240-60-20 = 160

假设余额为 160,它将保持 160 直到 for 循环结束。 我希望每次循环时,余额的值都会减少直到负值。 仅供参考,平衡变量不是局部变量。

有人知道如何解决这个问题吗? 这是代码的一部分。

// loop through each place
                        for (Place p : nearPlaces.results) {
                            balance = user_hour - duration - visit;
                            HashMap<String, String> map = new HashMap<String, String>();

                            //////////////////////////////////////////////////////////////////
                            googlePlaces = new GooglePlaces();
                            try {
                                placeDetails = googlePlaces.getPlaceDetails(p.reference);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            if(placeDetails != null){

                                String statuss = placeDetails.status;

                                // check place deatils status
                                // Check for all possible status
                                if(statuss.equals("OK")){




                                        lat = gps.getLatitude();
                                        lang = gps.getLongitude();
                                        double endlat = placeDetails.result.geometry.location.lat;
                                        double endlong = placeDetails.result.geometry.location.lng;



                                        Location locationA = new Location("point A");
                                        locationA.setLatitude(lat);
                                        locationA.setLongitude(lang);
                                        Location locationB = new Location("point B");
                                        locationB.setLatitude(endlat);
                                        locationB.setLongitude(endlong);
                                        double distance = locationA.distanceTo(locationB)/1000;
                                        Double dist = distance;
                                        Integer dist2 = dist.intValue();
                                        //p.distance = String.valueOf(dist2);
                                        p.distance = String.valueOf(balance);
                                        dist3 = p.distance;





                                }
                                else if(status.equals("ZERO_RESULTS")){
                                    alert.showAlertDialog(MainActivity.this, "Near Places",
                                            "Sorry no place found.",
                                            false);
                                }
                                }

                                ///////////////////////////////////////////////////////////////



                            if (balance > 0){

                                // Place reference won't display in listview - it will be hidden
                                // Place reference is used to get "place full details"
                                map.put(KEY_REFERENCE, p.reference);
                                // Place name
                                map.put(KEY_NAME, p.name);
                                map.put(KEY_DISTANCE, p.distance);
                                // adding HashMap to ArrayList
                                placesListItems.add(map);


                                }

                            else {
                                //

                            }


                        }//end for loop

【问题讨论】:

  • 了解正在发生的事情的最佳方法是使用 Eclipse 的调试工具跟踪您的代码。

标签: android eclipse google-maps for-loop google-places-api


【解决方案1】:

你到底想在这里做什么?

balance = user_hour - duration - visit; 在 for 循环后的第一行。我看不到user_hour、duration 或visit 的声明位置,但我假设它在循环之外。这意味着对于nearPlaces.results 中的每个Place,它始终是相同的值。如果此代码确实是您想要的,您不妨在循环之前声明它,因为您为每个Place 重新计算它是毫无意义的。

您也永远不会对 balance 进行任何操作,除非将其打印出来或为其设置其他值,因此很难计算出您期望发生的事情。

【讨论】:

  • user_hour、duration 或 visit 在循环外声明。当线路余额 = user_hour - 时长 - 访问时;在循环的第一行执行,余额会因为它是全局变量而改变吗?我希望每次在每个地方发生循环时都会更改平衡变量。
  • @HafizMughni 当您通过循环时,您不会修改user_hour、duration 或visit,因此balance 将始终是相同的值。
  • @ingo ya 你是对的..我对这件小事是多么粗心!!..非常感谢!
  • @HafizMughni 没问题。您想更新您的问题还是现在有效?
  • @ingo 现在可以工作了。只需将行更改为 balance = balance -user_hour - duration - visit;。有了这个,余额的价值就会像我想要的那样减少......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多