Search code examples
androidstringimagesettext

How to show Text depending of Image and Image's position


I have an array of Image ID's

        public Integer[] deckIDs = {
        R.drawable.bashnya,
        R.drawable.blazhenstvo,
        R.drawable.borba,
        R.drawable.card_tarot,
        R.drawable.deystvie,
        R.drawable.odinochestvo,
        R.drawable.opasnost,
        R.drawable.osoznanie,
        R.drawable.otshelnik,
        R.drawable.pamyat,
        R.drawable.pechal,
        R.drawable.peremirie,
        R.drawable.pobeda,
        R.drawable.pomoshch,
        R.drawable.poveshevyi,
        R.drawable.predlagaushchyi,
        R.drawable.presyschenie
};

I place these Images in ImageViews with a random

    Random card1 = new Random();
    int card1_value = card1.nextInt(16);
    int card2_value = card1.nextInt(16);
    int card3_value = card1.nextInt(16);
    int card4_value = card1.nextInt(16);
    int card5_value = card1.nextInt(16);

    card1_image.setImageResource(deckIDs[card1_value]);
    card2_image.setImageResource(deckIDs[card2_value]);
    card3_image.setImageResource(deckIDs[card3_value]);
    card4_image.setImageResource(deckIDs[card4_value]);
    card5_image.setImageResource(deckIDs[card5_value]);

Also I have 5 TextViews and a lot of strings like

<string name="card1_1">card 1 - text 1</string>
<string name="card1_2">card 1 - text 2</string>
<string name="card1_3"">card 1 - text 3</string>
<string name="card1_4"">card 1 - text 2</string>
<string name="card1_5"">card 1 - text 5</string>
<string name="card2_1"">card 2 - text 1</string>
<string name="card2_2"">card 2 - text 2</string>
...

And I need to show one of the string, depending on what image from array I've got, and in what position (1 of 5) it has placed. For example, one of the Images make card 1 - text 1 if it is in first position, and card 1 - text 2 if it is in seconds one. And so on

Hope you understand clearly my problem and you'll help me, because I stuck because of my weak programming skills.

I need to add, that there will be much more Images (near 80) and I hope, it possible to set text, depending on string-name and res id, like

           <string name="card44"">card 44 - text 1</string>

           R.drawable.card44 

There is a whole problem in this point. I just even don't know how to google


Solution

  • I decided my problem this way: card_value - random int for getting name of card from deckIDS array. This name is equal to the one of array's name in strings.xml. With method getIdentifier I get string value.

       String fortune_1 = getResources().getResourceEntryName(deckIDs[card1_value]);
    
        int resId_1 = getResources().getIdentifier(fortune_1, "array", getPackageName());
    
        String[] text_of_fortune_1 = getResources().getStringArray(resId_1);
    

    Hope it will be usefull.