Search code examples
javaandroidandroid-recyclerviewplaying-cards

imageButton in RecyclerView not displaying


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!


Solution

  • 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());