Roland
Roland

Reputation: 866

Why listInterator.hasNext() is always returning true?

I have a listInterator with only one element in it. I have a function thet need to know id it has next element. But the method hasNext() is always retourning true.

This code is inside a oncliclistener of a button. I just want: when the user click this button, if has next element change some image, if not change to another image.

    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);
        }

With my test, I doing with only one element in the array. The hasPreviuos() is woking ok. But hasNext() is retoutning true even with only one element on the array.

Upvotes: 0

Views: 330

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

You must implement lInterator.next() to forward cursor.

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);
        }

Upvotes: 1

Related Questions