I have one ScrollView defined in my XML, I have a LinearLayout which is Vertical (the one in code referenced as ll, or ID pickmovieLL)
I want to add Horizontal LinearLayouts under the vertical LinearLayout ll
I am doing this by going through a for loop of a List of genres (using this to get the text for checkboxes), basically I want to add two checkboxes left to right before going on a new line (I find the look of just 1 per line to be a bit odd looking with empty space)
LinearLayout ll = (LinearLayout) findViewById(R.id.pickmovieLL);
LinearLayout ll2;
CheckBox ch;
CheckBox ch2;
for (int i = 0; i < genres.size(); i++) {
if (genres.get(i) != null) {
ll2 = new LinearLayout(this);
ll2.setOrientation(LinearLayout.HORIZONTAL);
ll2.setLayoutParams(new LinearLayout.
LayoutParams(LinearLayout.LayoutParams.
MATCH_PARENT, LinearLayout.LayoutParams.
WRAP_CONTENT));
ll2.setGravity(Gravity.CENTER);
ll2.setWeightSum(1);
ch = new CheckBox(this);
ch.setLayoutParams((new LinearLayout.
LayoutParams(LinearLayout.LayoutParams.
MATCH_PARENT,LinearLayout.LayoutParams.
MATCH_PARENT,1)));
ch.setText(genres.get(i).genreName);
ch.setId(genres.get(i).getGenreID());
ll2.addView(ch);
}
if (genres.size() - i > 1){
ch2 = new CheckBox(this);
ch2.setLayoutParams(new LinearLayout.
LayoutParams(LinearLayout.LayoutParams.
MATCH_PARENT,LinearLayout.LayoutParams.
MATCH_PARENT,1));
ch2.setText(genres.get(i+1).genreName);
ch2.setId(genres.get(i+1).getGenreID());
ll2.addView(ch2);
i++;
}
ll.addView(ll2);
}
One problem I am having is that only three of the 5 genres I have in the list shows up. The list is Fantasy, Sci-Fi, Comedy, Drama and Action... Only Sci-Fi, Drama and Action show up. (I previously had issues with it going out of bounds for the list, hence why I am incrementing the for loop like this.), Been going over it over and over and cannot figure out what is wrong, have used hierarchy viewer and it just shows that its added to the right LinearLayout, but only one is added, I can't figure out why the first genre is never added, but Action is which would be under the ch Checkbox (I have a feeling something is overwriting something maybe, but not sure why this would be).
Have checked out plenty of other posts to figure this out, and I am guessing the issue is caused by wanting to add two checkboxes in one go maybe.
Aucun commentaire:
Enregistrer un commentaire