Mustafa
Mustafa

Reputation: 97

iterator has item but hasnext doesn't return true

enter image description here

while(iter.hasNext()) iterator has items but condition doesn't work. What's wrong there?

iter= productDataList.iterator();

public void CalcPrice(View v){
        while(iter.hasNext()){
            if(iter.next().name.equals(pName)){
                price=Integer.parseInt(iter.next().price);
            }
        }
        edPrice.setText("Tutar : "+price);
    }

Upvotes: 1

Views: 143

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 78985

The problem is that you are calling iter.next() twice for the same data.

Do it as follows:

iter= productDataList.iterator();

public void CalcPrice(View v){
    while(iter.hasNext()){
        Product productData = iter.next()
        if(productData.name.equals(pName)){
            price=Integer.parseInt(productData.price);
            break;
        }
    }
    edPrice.setText("Tutar : "+price);
}

Upvotes: 3

Related Questions