2013-10-07 133 views
0

我有一個只有一個元素的listInterator。我有一個函數需要知道它有下一個元素。但是hasNext()方法總是讓人回味。爲什麼listInterator.hasNext()總是返回true?

此代碼位於按鈕的oncliclistener中。我只想:當用戶點擊這個按鈕時,如果有下一個元素改變了一些圖像,如果沒有改變到另一個圖像。

ListIterator<String> lInterator = datas.listIterator(datas.size() - 1); 

    if(lInterator.hasNext()){ 
      imgDataProxima.setBackgroundResource(R.drawable.bt_next); 
     } else {      
      imgDataProxima.setBackgroundResource(R.drawable.btcheckin_next_disabled); 
     } 

     if(lInterator.hasPrevious()){ 
      imgDataAnterior.setBackgroundResource(R.drawable.bt_previous); 
     } else { 
      imgDataAnterior.setBackgroundResource(R.drawable.btcheckin_previous_disabled); 
     } 

用我的測試,我只用數組中的一個元素。 hasPreviuos()正在啓動。但是,即使數組中只有一個元素,hasNext()也會返回true。

+0

顯示您的codev – stinepike

+4

因爲您永遠不會移動迭代器的「位置」。 –

+0

此代碼位於按鈕的oncliclistener內。我只想:當用戶點擊這個按鈕時,如果有下一個元素改變了一些圖像,如果沒有改變到另一個圖像。 – Roland

回答

1

您必須執行lInterator.next()來轉發遊標。

if(lInterator.hasNext()){ 
      imgDataProxima.setBackgroundResource(R.drawable.bt_next); 
      lInterator.next(); // add this row 
     } else {      
      imgDataProxima.setBackgroundResource(R.drawable.btcheckin_next_disabled); 
     } 

     if(lInterator.hasPrevious()){ 
      imgDataAnterior.setBackgroundResource(R.drawable.bt_previous); 
     } else { 
      imgDataAnterior.setBackgroundResource(R.drawable.btcheckin_previous_disabled); 
     } 
+0

雖然不會goona工作。我定義了If/Else之類的東西。如果接下來它會改變圖像,如果沒有接下來它會改變到另一個圖像。 – Roland

+0

看到我的編輯,我加了 –

+0

我做了你說的,但它不起作用。這個函數在一個按鈕的oncliclistener中。 – Roland

相關問題