2

I've been a lurker on your forums for some time now, and they have helped me enormously on a variety of issues I've had, so thanks! :D

I'm trying to create my first app for android and it's a card game named shithead that my friends and I used to play often.

I've decided to use a RecyclerView to display your hand. I need to be able to dynamically add buttons (with card images) to the display at runtime. From what I can tell, I need an adapter for this.

Using the handy guide at "https:// guides.codepath.com/android/using-the-recyclerview" and attempting to modify it for my own purposes I've come very close to a test run of making this work. When I run the program on an emulator in Android Studio, it gives me the following display: images displayed as grey rectangles

I feel like I've got to be really close, and I'm simply missing some crucial syntax for working with android or android studio.

In my Card object I build a String idText that correlates to the card images I have saved in my project's mipmap-hdpi, mipmap-xhdpi etc. folders (mipmap-hdpi shown below) mipmap-hdpi folder in Android Studio

public Card(int suitInput, int rankInput)
{
    suit = suitInput;
    rank = rankInput;
    faceUp = false;
    text = RankString[rank] + " of " + SuitString[suit];
    idText = "i_" + RankString[rank] + "_of_" + SuitString[suit];
}

I also have a function getImageId in my Card Class:

public static int getImageId(Context context) {
    return context.getResources().getIdentifier("drawable/@+id/" + idText, null, context.getPackageName());
}

my onBindViewHolder method in my CardAdapter is below:

@Override
public void onBindViewHolder(CardAdapter.ViewHolder viewHolder, int position)
{
    //get the data model based on position
    Card card = mCards.get(position);

    //Set item views baased on views and data model
    ImageButton imageButton = viewHolder.cardButton;
    imageButton.setImageResource(card.getImageId(mContext));
    TextView textView = viewHolder.cardText;
    textView.setText(card.text);
}

my MainActivity class does very little as of yet. It initializes one player, me, and a Deck of Card objects. It then gives me 6 cards off the top of the unshuffled deck, AKA 2 - 7 of diamonds. It then initializes the CardAdapter.

public class MainActivity extends AppCompatActivity {

Player me;
Deck deck;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testdisplay);

    //lookup the recyclerview in activity layout
    RecyclerView rvCards = (RecyclerView) findViewById(R.id.rvCards);

    me = new Player("Dr_StrangeKill");
    deck = new Deck();

    me.hand.add(deck.getCards().remove(0));
    me.hand.add(deck.getCards().remove(0));
    me.hand.add(deck.getCards().remove(0));
    me.hand.add(deck.getCards().remove(0));
    me.hand.add(deck.getCards().remove(0));
    me.hand.add(deck.getCards().remove(0));

    //create adapter passing in sample user data
    CardAdapter adapter = new CardAdapter(this, me.hand);
    //Attach the adapter to the recyclerView to populate items
    rvCards.setAdapter(adapter);
    //Set layout manager to position the items
    rvCards.setLayoutManager(new LinearLayoutManager(this));
}

The Deck, Player, and Card objects all appear to be working as intended insofar as the values of the code are concerned, as the resultant display correctly shows that 'Dr_StrangeKill' has received 2 - 7 of Diamonds, but doesn't show the card images themselves! This is my first time working with images in any IDE (eclipse, visual studio, android studio), and I feel like i'm very close but off in some small but crucial way. Any help would be greatly appreciated!

  • Is anything displaying, or is it just the `ImageButton` that isn't? – JediBurrell Mar 29 '17 at 19:05
  • Why are you storing the images in the mipmap folder, but getting the resource id from drawable ? – nabil london Mar 29 '17 at 19:09
  • @JediBurrell - it displays small grey boxes that appear to respond to clicks (they flash) but no images inside them. I'm guessing that they are the smallest default button that android studio displays – Dr_StrangeKill Mar 29 '17 at 19:15
  • @nabillondon - Because I'm a total noob at this stuff. how should I reference the images? from what I can understand, referencing drawable allows android studio to properly pick the correct image size depending on the screen it's displaying to.... should I pick one? AKA return context.getResources().getIdentifier("mipmap-hdpi/@+id/" + idText, null, context.getPackageName()); – Dr_StrangeKill Mar 29 '17 at 19:16

2 Answers2

1

I suggest you put your images in a drawable folder because mipmap folder is for launcher icons, if you want to support different screen densities, here's a link that shows you how to create a drawable folder for each screen density, after that you should get your image identifier like this:

context.getResources().getIdentifier(idText, "drawable", context.getPackageName());
Community
  • 1
  • 1
nabil london
  • 511
  • 2
  • 13
  • So close and yet so far! The RecyclerView now displays cards, and they scroll as intended, but all the cards displayed are the Ace of Spades, which correlates to the final index of RankString and SuitString. the Card Objects being picked from the deck are still correct, as it's correctly displaying the text '2 of spades' etc. next to the image, but somehow the image referenced is always the ace of spades... ![aceofspades](http://imgur.com/baYBaEG). – Dr_StrangeKill Mar 29 '17 at 20:05
  • 1
    GOT IT! Both my idText field and my getIdText method were STATIC, while my text field was not. In the creation of the deck, the very last instantiated card was the ace of spades, and therefore each call to getIdText() would return the ace of spades. Thank you so much for your help! – Dr_StrangeKill Mar 29 '17 at 20:22
  • @Dr_StrangeKill Glad it helped :) please consider accepting it as answer – nabil london Mar 29 '17 at 20:28
0

It looks like you're trying to get the identifier of the image resource by name. In that case you could try:

return context.getResources().getIdentifier(idText, "mipmap", context.getPackageName());

But doing that is not recommended. See docs for getIdentifier() https://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)

You could just add the id (an int) to your card object and instantiate like new Card(R.mipmap.hearts_2,2,"hearts") or however you're instantiating (seems like you're using public fields and no constructor) and grab that id in your adapter to set on the imageview.

andrewoid
  • 76
  • 3