Austin Lazarus Austin Lazarus - 1 month ago 12
Android Question

How to Iterate through a Java Arraylist one at a time

How can I get to iterate through this ArrayList algorithm so that I can change the background of an imageView every time I input a code... E.g. Code = "123456" | Background_1 shows, Code = "123456" | Background_2 shows ...

enter = (ImageButton) findViewById(R.id.enter);
input = (EditText) findViewById(R.id.editText2);
punch_back_1 = (ImageView) findViewById(R.id.imageView6);
punch_back_2 = (ImageView) findViewById(R.id.imageView7);
punch_back_3 = (ImageView) findViewById(R.id.imageView8);
punch_back_4 = (ImageView) findViewById(R.id.imageView9);
final ArrayList<ImageView> array_image = new ArrayList<>();
array_image.add(punch_back_1);
array_image.add(punch_back_2);
array_image.add(punch_back_3);
array_image.add(punch_back_4);
enter.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
String code = input.getText().toString();
int i = 0;
if (code.equals("123456")) {
i++;
array_image.get(i).setImageResource(R.drawable.punch_front);
Toast.makeText(getApplication(), "Awarded 1 new punch", Toast.LENGTH_LONG).show();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
input.setText(" ");
}

return false;
}
});


I've tried this both:

for (int i = 0; i < 4; i++)
{

if (code.equals("123456"))
{
array_image.get(i).setImageResource(R.drawable.punch_front);
Toast.makeText(getApplication(), "Awarded 1 new punch", Toast.LENGTH_LONG).show();
}

}


int i = 0;
while (i < i)
{
array_image.get(i).setImageResource(R.drawable.punch_front);
Toast.makeText(getApplication(), "Awarded 1 new punch", Toast.LENGTH_LONG).show();
i++;
}


Which, if I am not mistaken does the same thing.

The below line of code eventually worked for me. onTouch Action_UP was my culprit. Using input.setText(null); did the same effect I wanted and preserved my sanity...

enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String code = input.getText().toString();
if (code.equals("123456") && index < array_image.size()) {
array_image.get(index).setImageResource(R.drawable.punch_front);
index++;
Toast.makeText(getApplication(), "Awarded " + index + " new punch", Toast.LENGTH_LONG).show();
input.setText(null);
}
}
});

Answer

EDIT: I seem to have misunderstood your question. It looks as if you want to have the image appear only once per fire event.

Doing this would require you do two things.

  1. Keep count of the index at which you'll be changing.
  2. Increment the index after use.

Also, you'll need to insure you do not go out of bounds as well of course.

First create a counter for the index as a member.

private int index = 0;

Next you'll want to do what you were doing before almost. In your event listener:

if(code.equals("123456") && index < array_image.size())
{
    array_image.get(index).setImageResource(R.drawable.punch_front);
    index++;
}

This should keep track of the current index, increment its position for the next event, and after you've gone through your items in the array, it shouldn't give you any exception errors when you go out of bounds and try to fire the event again.

Comments