0

I am new to Android and trying to create my first app. It should have an ImageView along with two buttons (Back and Next). When the user clicks on the Next button, the image in the ImageView should be replaced with the next image (hosted on my server). The names of the files are 1.jpg, 2.jpg, 3.jpg... I am using the following code, but something is not working. When the Activity starts the first image is loaded properly, but when I click the Next button nothing happens (nothing in LogCat also).

public class slidesActivity extends Activity {

private ImageView imageView;
private int imageNumber = 1;
private String plakatiUrl = "http://plakati.bg/" + 
                                 Integer.toString(imageNumber) + ".jpg";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.slides);

    final Drawable image = LoadImageFromWeb(plakatiUrl);
    imageView = new ImageView(getBaseContext());
    imageView = (ImageView) findViewById(R.id.imageView1);
    imageView.setImageDrawable(image);

    Button nextButton = (Button) findViewById(R.id.nextBtn);
    nextButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            imageNumber++;

            // Have to find why
            imageView.setImageDrawable(image);
            // is not working here
        }
    });

private Drawable LoadImageFromWeb(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        System.out.println("Exc=" + e);
        return null;
    }
}
}

I know that I have to make this in a different thread, so I don't get a ANR, but still I am missing something here. Could anyone help me with this, please !

slybloty
  • 6,346
  • 6
  • 49
  • 70
Iliya Popov
  • 103
  • 2

2 Answers2

0

Your onClick() method reloads the same image every time it's tapped. You need to modify your image and its link. Change the LoadImageFromWeb(String) to

LoadImageFromWeb(String url, int imageNumber) 
{
   String plakatiUrl = url + Integer.toString(imageNumber) + ".jpg";
   //...
}

And call it from the onClick() method.

slybloty
  • 6,346
  • 6
  • 49
  • 70
  • Hi, thanks for the help. I tried that, but I am still missing something. Am I not calling the method with: imageView.setImageDrawable(image); – Iliya Popov Mar 08 '12 at 16:53
0

This works for me. You can clean it up, though.

public class TestAppActivity extends Activity {

    private ImageView imageView;
    private int imageNumber = 1;
    private String plakatiUrl = "http://plakati.bg/";
    private Drawable image = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slides);

        image = LoadImageFromWeb(generateUrlString(plakatiUrl, imageNumber));
        imageView = new ImageView(getBaseContext());
        imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setImageDrawable(image);

        Button nextButton = (Button) findViewById(R.id.nextBtn);
        nextButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                imageNumber++;

                image = LoadImageFromWeb(generateUrlString(plakatiUrl, imageNumber));

                imageView.setImageDrawable(image);
            }
        });

    }

    private Drawable LoadImageFromWeb(String url) {
        Drawable d = null;

        InputStream is;
        try {
            is = (InputStream) new URL(url).getContent();
            d = Drawable.createFromStream(is, "src name");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return d;
    }

    private String generateUrlString(String str, int n) {

        return str + Integer.toString(n) + ".jpg";
    }
}

Also, I recommend using Bitmap instead of Drawable. And, I've noticed you put the LoadImageFromWeb() method inside onCreate().

slybloty
  • 6,346
  • 6
  • 49
  • 70
  • Great !!! It is working now! Thanks for the help. What is the benefit of using Bitmap instead of Drawable? – Iliya Popov Mar 08 '12 at 19:04
  • Check these links: [link](http://stackoverflow.com/a/3375258/935627) and [link](http://stackoverflow.com/a/5877202/935627) – slybloty Mar 08 '12 at 19:13