Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Can you not achieve the addition of these child views to a parent view with a ListView and Adapter? –  Jonathan Viccary 19 hours ago

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
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 –  K20GH 19 hours ago
    
You would need an adapter for the ListView that returns your CardView in it's getView() method - developer.android.com/reference/android/widget/… –  Karl 19 hours ago
    
What would be the benefit of using a List view over this method? –  K20GH 19 hours ago
1  
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 :) –  Karl 15 hours ago

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);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.