Display A Random Image When A Button Is Clicked
Solution 1:
Try this way
int[] cards={R.drawable.card1,R.drawable.card2,R.drawable.card3,R.drawable.card4,R.drawable.card5,R.drawable.card6,R.drawable.card7};
Randomr=newRandom();
int n=r.nextInt(7);
imageview.setImageResource(cards[n]);
Solution 2:
Your question is kind of ambiguous, but with whatever little I understood, I suggest,if you are sure of having only 7 decks, why dont you hardcode them and assign a value to each of them. So that when you call random function, it will check which number is the result, suppose it is 5, then call setDrawableResource(R.drawable.img5) and so on.
Solution 3:
You should be keeping the cards in an array or List
of some kind. Then, you can refer to them by their number. For example:
ArrayList<Card> deck = new ArraList<Card>();
//Fill the ArrayList. Maybe shuffle it.
selectedCard = ArrayList.get(randomNumber);
Card
could simply be String
or something, instead. I don't know what sort of object you're using for it.
Solution 4:
You shoud be using ImageView instead of MediaPlayer. You should assigned 7 images(R.drawables.mythos1, ...) assigened to 1-7 and set it to ImageView imageView.setDrawableResource(R.drawable.myths1);
depending on the random number. Look at example from here Get the ID of a drawable in ImageView
Post a Comment for "Display A Random Image When A Button Is Clicked"