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 !