2017-01-31 58 views
-5

這是我ArrayList循環數組列表中

{10, 20, 30,40, 50, 60, 70, 80, 90, 100....1000} 

我想環路上的按鈕,點擊我的結果。

我想要第一個5結果在第一個Button的點擊。

然後自動下一個5結果同樣。

有什麼辦法可以做到嗎?

for(int i=0 ;i<5; i++) { 
    list = array.getJSONObject(i); 
    fr.add(list.get("contact")); 
} 

問題是,我只得到第一個5沒有下一個結果。

感謝您的幫助。

+1

不是從0開始迭代,而是從'buttonClicks * 5'開始迭代。結束迭代不是在5,而是在'buttonClicks * 5 + 5' –

+0

,因爲你只循環前五個元素 –

+0

,所以你期望從那得到什麼結果? –

回答

0

那麼,你正在嘗試閱讀前5個元素總是因此問題。您已閱讀並從陣列列表中刪除元素的一個選項。但更好的方法是保持點擊鼠標的數量,在這種情況下,它是一個經典的分頁問題,​​你可以獲取接下來的5個元素,並且不會丟失arraylist中的數據。

1

如果你想每次調用從到索引A指數的方法及時清除數組列表中的第N項,

您可以使用ArrayList.removeRange(int fromIndex, int toIndex)每次按下該按鈕(但不要重複這種)

或輕鬆使用ArrayList.subList(start, end).clear();進行相同的任務!

從此列表中刪除索引在 fromIndex(包含)和toIndex(不包括)之間的所有元素。將任何後續的 元素向左移(減少它們的索引)。此調用通過(toIndex - fromIndex)元素縮短 列表。 (如果toIndex == fromIndex,則此操作 沒有影響。)

讀取的原稿>http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#removeRange%28int,%20int%29

例如:

每次點擊後
public void removeFistNItemsFromList(){ 

    for(int i=0 ;i<n; i++) { 
    // add your items or do what ever you want! 
    } 
    myArray.subList(0, 4).clear(); // here from 0 to 4th index items will be removed and list will be updated! 
    System.out.println("MyArrayListNow:"+myArray); 
} 

輸出:

enter image description here