Trying To Sort An Array To Be Displayed In Table Rows
Solution 1:
unless it's serving some other purpose, your code is overcomplicated for what you're doing. You can just iterate through listAnswers
directly to populate another list with the new sort.
Think about adding elements from listAnswers
to a new list newList
in order. First, you want to add the 0th element followed by the 4th, then the 1st followed by the 5th, then the 2nd followed by the 6th, then the 3rd (which would be followed by the 7th, but in your example there is no 7th element).
So we've looped through 4 times, each time adding a pair of elements to the new list (except possibly on the last loop). Notice that each of the pairs of elements are offset by 4, which is equal to size of the bigger half of listAnswers
. We can calculate this by taking (size of the list + 1)/2 (try a few examples if you're not convinced).
In code:
List<HolderAnswer> newList = new ArrayList<HolderAnswer>();
int loop = 0;
int offset = (listAnswers.size() + 1) / 2;
while (newList.size() < listAnswers.size()) {
newList.add(listAnswers.get(loop);
if (newList.size() < listAnswers.size()) {
newList.add(listAnswers.get(loop + offset);
}
loop += 1;
}
EDIT: ok, I see what you mean. Your code looks close, but you need to add the elements consecutively to the ArrayList<HolderAnswer>
s in listAnswersSorted
. There may be an easier way, but I did this by keeping three variables index
, row
, and col
, which represent the iteration through listAnswers, the row, and the column in the 2D array 'listAnswersSorted', respectively.
ArrayList<HolderAnswer> listAnswers = getAnswers();
ArrayList<ArrayList<HolderAnswer>> listAnswersSorted =
new ArrayList<ArrayList<HolderAnswer>>();
// initialize the ArrayLists in listAnswersSortedint numRows = listAnswers.size() / numColumns + 1;
for (int i = 0; i < numRows; i += 1) {
listAnswersSorted.add(new ArrayList<HolderAnswer>());
}
// calculate column index where the "step" happensint step = listAnswers.size() % numColumns;
// loop through and add elements to listAnswersSortedint index = 0;
int row = 0;
int col = 0;
while (index < listAnswers.size()) {
listAnswersSorted.get(row).add(listAnswers.get(index));
int rows = col < step ? numRows : numRows - 1;
row += 1;
if (row == rows) {
row = 0;
col += 1;
}
index += 1;
}
// flatten the ArrayList<ArrayList> into a single ArrayList
ArrayList<HolderAnswer> newList = new ArrayList<HolderAnswer>();
for (ArrayList<HolderAnswer> list : listAnswersSorted) {
newList.addAll(list);
}
Solution 2:
I get the feeling you are going about it the "wrong" way. If you are working with TableLayout, then you don't need to think ahead of how TableLayout works internally. Just sort the array how you want (desc/asc etc) and build your table after that. You define your table with how many columns and then all you do is add rows.
In your sorted example you have two columns. Define 2 columns (this can be done dynamically, depending on your data) and add rows and the results would be just like you have there.
I'll give a code example:
for (inti=0; i < listAnswersSorted.size(); i++) {
TableRowtable_row=newTableRow(this);
TextViewtv=newTextView(this);
table_row.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
table_row.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setText(listAnswersSorted.get(i));
table_row.addView(tv);
}
...
table.addView(table_row);
Hope you solve the problem in any case.
Post a Comment for "Trying To Sort An Array To Be Displayed In Table Rows"