0

I've got a layout which is called activity_main.xml which is my parent layout, and I am then inserting a child layout (activity_main_card.xml) within a for loop.

I also have an array, and what I am doing is using the length of the array to determine how many child elements it should create. All of my data to be used in the child element is stored in the array, so the idea is to loop through the array, create a child element for each loop and populate the data.

Instead, what is currently happening is that it is generating the 3 (length of array) child elements, but it is only populating the first one with the latest content in the array. This is because it keeps the variables the same.

What I need to do is somehow set dynamic variables that change as the loop iterates.

The code for my method is as follows:

    for (int i = 0; i < cardArray.length; i++) {
        LinearLayout item = (LinearLayout)findViewById(R.id.card_holder);
        View child = getLayoutInflater().inflate(R.layout.activity_main_card, item, false);
        item.addView(child);

        //Set objects from array to variables
        String cardTitle = cardArray[i][0];
        String cardContent = cardArray[i][1];
        String cardImage = cardArray[i][2];

        //Set XML elements to variables
        TextView title = (TextView) findViewById(R.id.card_title);
        TextView content = (TextView) findViewById(R.id.card_content);
        ImageView image = (ImageView) findViewById(R.id.card_image);

        //Load variable content into card layout
        title.setText(cardTitle);
        content.setText(cardContent);
        image.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/" + cardImage, "drawable", getPackageName())));
        }

What I thought I could do was to set the views like:

TextView title[i] = (TextView) findViewById(R.id.card_title); 

however this doesn't work. Does anyone know how I can acheive this?

1
  • Can you not achieve the addition of these child views to a parent view with a ListView and Adapter? Commented Sep 3, 2014 at 21:59

2 Answers 2

1

You need to get a reference to the current View you just created and then populate the TextView elements on that View. This is achievable by doing this:

TextView title = (TextView) child.findViewById(R.id.card_title);
TextView content = (TextView) child.findViewById(R.id.card_content);
ImageView image = (ImageView) child.findViewById(R.id.card_image);

You might be better off approaching this problem differently however, as Jonathan suggests a ListView and adapter might suit better for this situation.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. This worked as a "quick fix", however I'll take a look into ListViews and Adapters instead. I assume I can assign any view to a ListView, as I am using the new CardView from L
You would need an adapter for the ListView that returns your CardView in it's getView() method - developer.android.com/reference/android/widget/…
What would be the benefit of using a List view over this method?
In short: you load all your objects into an Adapter, and then tell the ListView to use said adapter. You're doing the same thing essentially but ListView/Adapter would streamline the process, it's also very scalable. Probably also comes down to preference at this point :)
0

You aren't using the View created, you are using the root layout of the activity in every loop because you apply the findViewById in the parent view:

Change:

TextView title = (TextView) findViewById(R.id.card_title);

By;

TextView title = (TextView) child.findViewById(R.id.card_title);

Comments

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.