0

I am trying to make a line of hearts indicating the number of lives the player currently has. This is my code:

LifeGraphics.java

public class LifeGraphics {
    public static ArrayList<ImageView> getLiast(Context context){
        ImageView life1 = new ImageView(context);
        life1.setImageResource(R.mipmap.heart);
        ImageView life2 = new ImageView(context);
        life1.setImageResource(R.mipmap.heart);
        ImageView life3 = new ImageView(context);
        life1.setImageResource(R.mipmap.heart);
        ImageView life4 = new ImageView(context);
        life1.setImageResource(R.mipmap.heart);
        ImageView life5 = new ImageView(context);
        life1.setImageResource(R.mipmap.heart);
        ImageView[] inRetrunList = new ImageView[] {life1, life2, life3, life4, life5};
        ArrayList<ImageView> returnList = new ArrayList<>();
        returnList.addAll(Arrays.asList(inRetrunList));
        return returnList;
    }
}

GameActivity.java (Excerpt):

ArrayList<ImageView> livesGraphicsList = LifeGraphics.getLiast(getApplicationContext());
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
for(int i = 0; i < lives; i++){
    layout.addView(livesGraphicsList.get(i), (i + 1));
}

Here is the result:

Only one heart is shown

When they lose a life the number goes down properly and the image goes away after the last life. also logging the layout.getChildCount() gives me 6 (the textview and the 5 added imageView i guess). Can someone please explain why the are not showing on screen.

Thank you in advance.

1 Answer 1

2

You are setting the image only to the first ImageView. To be more precise you set it 5 times.

    ImageView life1 = new ImageView(context);
    life1.setImageResource(R.mipmap.heart);
    ImageView life2 = new ImageView(context);
    life1.setImageResource(R.mipmap.heart); //Should be life2
    ImageView life3 = new ImageView(context);
    life1.setImageResource(R.mipmap.heart);//Should be life3
    ImageView life4 = new ImageView(context);
    life1.setImageResource(R.mipmap.heart);//Should be life4
    ImageView life5 = new ImageView(context);
    life1.setImageResource(R.mipmap.heart);//Should be life5
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you i did not see that. I will accept answer when it lets me. I owe you one FINDarkside

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.