0

I have this code :

private ImageView d1;
private ArrayList<Integer> listaImagenes = new ArrayList<Integer>();
private ArrayList<String> listaFrases = new ArrayList<String>();
private Button button;
private Integer contador = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.rellenarImagenes();
    setContentView(R.layout.imagentonas);
    d1 = (ImageView) findViewById(R.id.imagenes01);
    while (contador < listaImagenes.size()) {
        d1.setImageResource(listaImagenes.get(contador));
        button = (Button) findViewById(R.id.botoncillo);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                contador++;
            }
        });
    }
}

private void rellenarImagenes() {
    listaImagenes.add(R.drawable.a01);
    listaImagenes.add(R.drawable.a02);
    listaImagenes.add(R.drawable.a03)
}

I am trying do a loop that when I press the button , increment contador and d1 change image. but it is not working, application background remains black and not working.

0

3 Answers 3

1

remove while loop and setimage in onclick method.

Sign up to request clarification or add additional context in comments.

Comments

0

You are expecting that modifying the value of the variable contador would result in the array item to change.

Keep in mind that in the code line d1.setImageResource(listaImagenes.get(contador));, the get function receives an int. So at the time where it's called it receives a value, not a reference to an Integer. When you change the value of contador, the value that was used to obtain the index in the array is not changed.

And even if the value did change, d1 would still be using the same resource.

What you need to do in the onClickListener is add the code to set the image. Something along the lines of

public void onClick(View v) {
    ++contador;
    if (contador >= listaImagenes.size())
    {
        contador=0;
    }
    //you'll probably need to modify the next line to be able to access the button variable.
    //one way to do it is to use a final variable in the onCreate method that creates this OnClickListener
    button.setImageResource(listaImagenes.get(contador));
}

The while loop is not needed. What your code is doing is setting the image to the 3 items of the array, one after the other, and adding a new click listener 3 times.

Comments

0

I will try to answer and point some of the flaws you have in the code.

  1. wat if there were 100 drawable like R01 ,R02...? Instead you can use getting drawable using string.
  2. why are you using while loop ? since you have the counter you can directly use that.

Let me try to write the code

int contador=1;
@Override
 public void onCreate (Bundle savedInstanceState)
 {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.imagentonas);
          context=this;
          d1=(ImageView)findViewById(R.id.imagenes01);
          button=(Button)findViewById(R.id.botoncillo); 
          button=(Button)findViewById(R.id.botoncillo); 

          button.setOnClickListener( new  View . OnClickListener ()  { 
            public  void onClick ( View v )  { 
                    int id = context.getResources().getIdentifier("a"+contador,
                             "drawable", context.getPackageName());  
                    d1.setImageResource(id);        
                    contador++;
             } 
          }); 
  }

Notice : int id = context.getResources().getIdentifier("a"+contador,"drawable", context.getPackageName()); Here using the string you can access the drawable this solves the issue for any number of consecutive drawables.

Hope you get the concept...

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.