0

I would like to display images that are stored in an arraylist to an imageview.I am using picasso library to store the links.When i press the button next i want the picture to change to the next image.I am using a for loop but i get only the last element.Here is the code:

 ImageView image1;

Button bNext,
ArrayList<String>ll=new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fwtografies);
    image1=(ImageView)findViewById(R.id.image1);
    bNext=(Button)findViewById(R.id.bNext);
    ll.add("http://i.imgur.com/QoUeA2I.jpg");
    ll.add("http://i.imgur.com/21szRz9.jpg");
   ll.add("https://upload.wikimedia.org/wikipedia/en/e/ec/Clip_Poster.jpg");

    bBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent fwtografiesActivityIntent = new Intent(Fwtografies.this,MainActivity.class);
            Fwtografies.this.startActivity(fwtografiesActivityIntent);
        }
    });
    bNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for(int i=0;i<ll.size();i++){
                sp(ll.get(i),image1);
            }
        }
    });
    bPrevius.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            viewFlipper.showPrevious();
        }
    });

    sp("http://i.imgur.com/gijIKJO.jpg",image1);
    //sp("http://i.imgur.com/QoUeA2I.jpg",image2);
    //sp("http://i.imgur.com/21szRz9.jpg",image3);
}

public void sp(String a,ImageView b){
    Picasso
            .with(getApplicationContext())
            .load(a)
            .into(b);
}}

2 Answers 2

1

Everytime you click on Button "bnext" you are going through your whole for-loop and therefore only the last element of your list gets shown. Try:

int i = 0;     
bNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
              if(i<ll.size()){
                 sp(ll.get(i),image1);
                 i++;
              }
        }
    });
Sign up to request clarification or add additional context in comments.

3 Comments

this 1 is doing the job,but i can't understand why ifor loop gets all the elements instead of 1 by 1
The loop gets all elements 1 by 1, but it happens so fast, that you can not see it.
i found my mistake.I should not use for cause i can't stop it to each click.I got to use if in order to manage it
0

My mistake is that i used for loop where i should use if statment. This code works for my case:

int i=0;  bNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //viewFlipper.showNext();
            if(i<ll.size()){
                sp(ll.get(i),image1);
                i++;
            }`

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.