0

I created a arraylist as below:

ArrayList<String> mtsData3 = new ArrayList<String>();
    mtsData3.add("1");
    mtsData3.add("2");
    mtsData3.add("3");
    mtsData3.add("4");
    mtsData3.add("5");
    mtsData3.add("6");
    mtsData3.add("7");
    mtsData3.add("8");

And my code:

for(int i = 0; i < 4; i++){
        LinearLayout ll4 = new LinearLayout(this);
        ll4.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams lllp4 = new LinearLayout.LayoutParams(SCREEN_WIDTH/5, LayoutParams.WRAP_CONTENT);
        ll4.setLayoutParams(lllp4);

        ll3.addView(ll4);

        for(int i2 = 0; i2 < 2; i2++){
            TextView tv = new TextView(this);
            tv.setPadding(0,0,0,0);
            tv.setGravity(Gravity.CENTER);
            ViewGroup.LayoutParams lpTlB = new ViewGroup.LayoutParams(SCREEN_WIDTH/5, SCREEN_HEIGHT/20);
            tv.setLayoutParams(lpTlB);
            tv.setText(mtsData3.get(i2));
            ll4.addView(tv);
        }
    }

Now, I am getting only 1 and 2 values from ArrayList. I want all values from ArrayList.

Here's what I am getting:

1   1   1   1
2   2   2   2

And Here's what I want:

1   3   5   7
2   4   6   8

1 Answer 1

1
for(int i2 = 0; i2 < 2; i2++) {
    ...
    tv.setText(mtsData3.get(i2));
    ...
}

With this, i2 can flip back and forth between values 0 and 1. That is why you were getting 1,2,1,2...

You can have a separate variable for indexing into the list

...
int index = 0;
for(int i2 = 0; i2 < 2; i2++) {
    ...
    tv.setText(mtsData3.get(index++));
    ...
}

mtsData3.get(index++) - It gets the value at index index from mtsData3 and increments index.

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

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.