2014-10-03 47 views
-5

我有一個這樣的數組:的方法添加到一個ArrayList中退出

ArrayList<FuelData> arrValue = objrp.FuelReport(AccountID, 
        deviceID, datefrom, dateto, timezone, timefrom, timeto); 
      for (int i = 0; i < arrValue.size(); i++) { 
       String statusDescription = StatusCode.getDescription(AccountID, 
         arrValue.get(i).GetStatusCode(), privLabel, null); 
       String detail = "Mức nhiên liệu:" 
         + Math.round(arrValue.get(i).GetFuelLevel()) 
         + " l\\nQuãng đường đi được:" 
         + Math.round(arrValue.get(i).GetFuelLevel())// adding GetOdometer here 
         + " km\\nThời gian: " 
         + ConvertFromEpoch(arrValue.get(i).getTimeStamp()) 
         + "\\nĐịa điểm:" 
         + GetUTF8FromNCRDecimalString(arrValue.get(i) 
           .getAddress()) + "\\nTrạng thái: " 
         + GetUTF8FromNCRDecimalString(statusDescription); 

       strscr += "chartData.push({fuelLevel: " 
         + arrValue.get(i).GetFuelLevel() + ",odometerKm: " 
         + arrValue.get(i).GetOdometerKM() + ", detail:\"" 
         + detail + "\"});\n"; 
      } 

現在我想將這個方法添加:

GetOdometer(String accountID, String deviceID, String timestamp) 

這一行:+ Math.round(arrValue.get(i).GetFuelLevel())// adding GetOdometer here此法不屬於FuelData類,但在其他類。我可以這樣做,如果可以,我該怎麼辦?

+1

這不是'array'它是'ArrayList' – 2014-10-03 04:47:27

+1

創建另一個類的實例,然後使用'instance.getOdometer()'調用它 – Tirath 2014-10-03 04:48:32

+0

@Tirath我也這樣做了你的建議,但我仍然不能得到getOdometer()的值 – dailammoc 2014-10-03 04:53:06

回答

1

調用arrValue.get(i)FuelData類的對象提取到arrValue arraylist中的i th索引中。

現在,如果方法getOdometer()是內部FuelData定義 - 你應該可以使用下面的語句來訪問它

arrValue.get(i).getOdometer("value1", "value2", "value3") 

確保getOdometer方法返回一個字符串或至少它返回的東西。

另外,正如我注意到你mentiioned這種方法沒有在FuelData定義。那麼你應該罰款做以下

//create Instance Of This Other Class before the for loop 
//then call `GetOdometer(String accountID, String deviceID, String timestamp)` 
//make sure GetOdometer is returning something 
//follow camel casing for naming methods 

例如,

///create instance 
OtherClass c = new otherClass(); 
for (int i = 0; i < arrValue.size(); i++) { 
. 
. 
String detail = "Mức nhiên liệu:" 
       + Math.round(arrValue.get(i).GetFuelLevel()) 
       + c.GetOdometer("value1", "value2", "value3") 

嘗試上面的代碼改變只有當GetFuelLevel方法的行爲是同一本OtherClass的所有對象。否則移動

OtherClass c = new otherClass(); 

裏面的for循環。如果它不起作用,請在評論中告訴我。

+1

我遵循第二種方式,它正常工作,謝謝 – dailammoc 2014-10-03 09:01:38

相關問題