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
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.