1

I tried earlier and just got more confused so i will try and be more precise. I am making an app in which i have a deck of 7 cards. I want to click on the deck and have one of the 7 cards pop up on the screen. So far I have

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

        final MediaPlayer mpClick = MediaPlayer.create(this, R.raw.click);
        randomM = (EditText) findViewById(R.id.randomM);

        //button 1 start        
        Button bMythos = (Button) findViewById(R.id.mythos);
        bMythos.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mpClick.start();
                Random r = new Random();
                int n=r.nextInt(7) + 1;
                randomM.setText(String.valueOf(n));
            }
        });
        //button 1 end
    }

}

So far this displays the card deck which i click on and a random number is generated (the text box is nearly for me to know the random number generator is working; will be removed when i figure out the display).

So my question How can i get the random number to correspond with a random card and get the card displayed? - the cards are labeled mythos1, mythos2, etc so i assumed i could do something with mythos(String.valueOf(n)) but that didn't work (unless i'm doing something else wrong) [if you can't tell i have no idea what i'm doing]

bribrem
  • 37
  • 1
  • 5
  • You want to generate any card from the 7 you display on screen ? – Naresh Sharma Apr 13 '12 at 05:41
  • Hmm... @Rashmi.B is right. The question is vague. I've answered a totally different question to everyone else. You're obviously new at this, but if you can clarify exactly what you're asking, that would help. :) – AlbeyAmakiir Apr 13 '12 at 05:49

4 Answers4

7

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};
 Random r = new Random();
 int n=r.nextInt(7);
 imageview.setImageResource(cards[n]);
Khan
  • 7,585
  • 3
  • 27
  • 44
0

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.

Rashmi.B
  • 1,787
  • 2
  • 18
  • 34
0

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.

AlbeyAmakiir
  • 2,217
  • 6
  • 25
  • 48
0

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

Community
  • 1
  • 1
Win Myo Htet
  • 5,377
  • 3
  • 38
  • 56