2013-10-10 22 views
0

我正在做一些列車應用程序。在應用程序中,我維護着火車時間。從我的數據庫,我可以能夠獲取倍這樣如何在android中將相應的分鐘與小時分組?

train_schedule_time------>[8.2, 13.55, 0.45] 

現在我想用這樣的對應分鐘小時,隔離我的數組String數組列表...

enter image description here

我使用StringTokenizer並分割成小時數組和分數組。但我無法用多個分鐘將我的小時分組,並且必須以列表視圖顯示。我怎樣才能做到這一點?有誰能夠幫助我?在此先感謝

+0

你的代碼在哪裏>? –

+0

我不確定你遇到什麼麻煩,是列表視圖嗎?如果你的stringtokenizer工作正常,以產生小時和分鐘數組,你可以發佈這些,以及你期望能夠做什麼/你不能做什麼? –

回答

2

您可以拆分您的字符串。例如:

String train_schedule_tim = "8.2, 13.55, 0.45"; 

String[] hours = train_schedule_tim.split(", "); 

String hour1 = hours[0].split(".")[0]; 
String mins1 = hours[0].split(".")[1]; 

String hour2 = hours[1].split(".")[0]; 
String mins2 = hours[1].split(".")[1]; 

如果你想保持一個后羿用幾分鐘的時間,你可以做這樣的事情(使用整數或字符串的是你喜歡什麼):

Map<Integer, List<Integer>> hours = new HashMap<Integer, List<Integer>>(); 

List<Integer> minutes = new ArrayList<Integer>(); 
minutes.add(15); 
minutes.add(30); 
minutes.add(45); 

hours.put(8, minutes); 

然後,可以這樣做:

for (Integer h : hours.keySet()) { 
    List<Integer> mins = hours.get(h); 
} 
+0

像這樣你可以得到你的小時和分鐘。這是你的問題嗎?問候 –

+0

不,謝謝你的回答。但是我的需要是我有幾分鐘的時間,比如train_schedule_time ------> [8.2,8.55,8.45]。如果我有這樣的價值觀意味着我怎樣才能將我的時間與相應的時間分組? – malavika

+0

好吧,我會編輯我的答案;-) –

0

例如,你可以使用StringTokenizer類(從java.util):

StringTokenizer tokens = new StringTokenizer(CurrentString, ":"); 
String first = tokens.nextToken();// this will contain "hours" 
String second = tokens.nextToken();// this will contain "Minutes" 
// in the case above I assumed the string has always that syntax (12:30) 
// but you may want to check if there are tokens or not using the hasMoreTokens method 
相關問題