2017-03-02 90 views
0

我想在適當的最小整數值時做一些說明。例如:當變量「round_min」= 10 - >有一些指令。當「round_min」= 100時 - >有另一條指令。HashMap:它在TextView中顯示最小值,但min_variable有更多值

我試着實現在HashMap中找到最小值的函數。 例如:我在HashMap中有兩個元素。並且找到最小值,並且正確顯示在TextView中。

但是,當我想要使用這個變量「round_min」到另一個函數時,在hashmap中顯示了所有的值。我想始終保持最低限度。

下面我將介紹代碼:

int round_min; 

// nearestDistances is a hashmap where I put distances 
Map.Entry<String, Float> nearestMarker = null; 

for (Map.Entry<String, Float> entry : nearestDistances.entrySet()) {  
    if (nearestMarker == null || nearestMarker.getValue() > entry.getValue()) { 
     nearestMarker = entry; 
     round_min = Math.round(nearestMarker.getValue()); 
     Log.e(" MIN ", round_min + ""); 

     // it displays in log.e all values 

     // this function see all values, not only minimum 
     DisplayMinimumValue(getApplicationContext(), round_min); 
     Log.e(" ROUND MIN ", round_min + ""); 

     tvDistanceToNearest.setText(String.valueOf(round_min)); // there is displaying only minimum value 
    } 
} 

有人能幫助我找到的問題,以及如何用它弄清楚?

回答

1

把所有的線

// this function see all values, not only minimum 
    DisplayMinimumValue(getApplicationContext(), round_min); 
    Log.e(" ROUND MIN ", round_min + ""); 

    tvDistanceToNearest.setText(String.valueOf(round_min)); // there is displaying only minimum value 

末,經過第二}讓您在循環外

+0

謝謝。有用 :) – gryzek

1

因爲我還不能評論...我不確定你的問題是什麼。如果您在地圖中遇到較小的值,我發現您正在更新您的round_min變量。我也看到你有一些在迭代中使用這個變量的方法。該計劃的哪部分不是像你想要的那樣行事?

+0

當我使用方法:「DisplayMinimumValue(背景下,round_min)它讓我在所有的值我不明白爲什麼它不能顯示最小值,但是當我在我的應用程序中使用tvDistanceToNearest(TextView)時,它只顯示了textView中的最小值,我希望在將「round_min」上面的方法將只有一個minum值,現在把所有的值都放到hashmap中,如何解決它? – gryzek

+0

DisplayMinimumValue用於insi你的循環,它會顯示任何值min_round分配給,所以每次更新。如果你只想顯示最後的最小值,你必須在循環之外調用它。 –

+0

是的,它幫助。但我只能標出一個正確的答案。也謝謝你的幫助。問候:) – gryzek