CodeCoolMath
CodeCoolMath

Reputation: 43

Kotlin list iterator jumps to last element

I wrote in code comments primarily whats going on with the code but here is a little more indepth. I created an array of drawable resources (images). I created a button and assigned its function to switch to the next element of the array with a click. ( Once its at the last element I intend to have it go back to element[0] but I haven't gotten that far yet).

I'm pretty new to android development so I hope this isn't a silly question. when I run the app and click the button, it jumps to the very last element. I also noticed it does not even show the first element on the screen ( just blank, then on a button click you see last element). below is the picture of whats happening:

p.s. there used to be more array elements but I deleted them to see if they were the problem.

p.s.s here is the actual code

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)



        //here I created an array of drawable images to pick from for the hair options
        val blankHairStyleList = arrayOf(R.drawable.hairponytailpink ,  R.drawable.hairponytailblue)

        //here I created an iterator which allows me to move through the array.
       val blankHairStyleListIterator = blankHairStyleList.iterator()

        //created button's click action
        findViewById<Button>(R.id.HairButton).setOnClickListener{

            //here is the actual iterator function, I feel like something is missing above here.
          while(blankHairStyleListIterator.hasNext()){
              findViewById<ImageView>(R.id.picture).setBackgroundResource(blankHairStyleListIterator.next())
          }
        }

photo of whats happeneing

Upvotes: 1

Views: 1151

Answers (2)

Priyanka
Priyanka

Reputation: 1875

You want something else as per your requirement and your code is doing a different thing. As per your code, you are setting image background until your iterator has reached last item as below -

 findViewById<Button>(R.id.HairButton).setOnClickListener{
      while(blankHairStyleListIterator.hasNext()){
        findViewById<ImageView>(R.id.picture).setBackgroundResource(blankHairStyleListIterator.next())
      }
    } 

Because you are using while so code will set last item as background by skipping all of the previous items.

You need to make change in your code as per your requirement as below -

findViewById<Button>(R.id.HairButton).setOnClickListener{
  if(blankHairStyleListIterator.hasNext()){
    findViewById<ImageView>(R.id.picture).setBackgroundResource(blankHairStyleListIterator.next())
  }
} 

Upvotes: 1

Salvatore Testa
Salvatore Testa

Reputation: 317

This is a great start! Here's how you can fix it up.

I created a button and assigned its function to switch to the next element of the array with a click.

What you have right now will always cycle to the end because the while loop will keep running as long as #hasNext is true.

while (blankHairStyleListIterator.hasNext()) {
  findViewById<ImageView>(R.id.picture).setBackgroundResource(blankHairStyleListIterator.next())
}

If you change the while to an if, the block will only run once!

if (blankHairStyleListIterator.hasNext()) {
  findViewById<ImageView>(R.id.picture).setBackgroundResource(blankHairStyleListIterator.next())
}

Once its at the last element I intend to have it go back to element[0] but I haven't gotten that far yet.

Once you have that if statement, you can use the else to create a new iterator (after you make the blankHairStyleListIterator a var instead of a val).

if (blankHairStyleListIterator.hasNext()) {
  findViewById<ImageView>(R.id.picture).setBackgroundResource(blankHairStyleListIterator.next())
} else {
  findViewById<ImageView>(R.id.picture).setBackgroundResource(blankHairStyleList.first())
  blankHairStyleListIterator = blankHairStyleList.iterator()
}

I also noticed it does not even show the first element on the screen (just blank, then on a button click you see last element).

You need to set the element on the ImageView. You can put something like this before the setOnClickListener.

findViewById<ImageView>(R.id.picture).setBackgroundResource(blankHairStyleList.first())

Please let me know if this does the trick for you!

Upvotes: 1

Related Questions