Search code examples
javaandroidandroid-tablelayout

TableRow is giving reverse TextViews


I am developing app in which I am showing attendance of students in a monthly manner with help of table. I have array of attendance values as'p' and 'a' and applied logic to show colors accordingly. I am creating a tableRow and TextView elements from java file. like the image here: Attendance_Image I want to show the days of month as 1,2,3,... upto last day. But the image is showing the numbers in reverse order. Here is the java code I am using to get that views.

int daysCounter = 0;
for(int j=0; j<5; j++) {
    TableLayout tableLayout = (TableLayout)findViewById(R.id.tl);
    TableRow tableRow = new TableRow(AttendanceActivity.this);
    TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    tableRow.setLayoutParams(layoutParams);
    //days variable contains the no. of days in that month
    for (int i=0; i<7 && daysCounter<days; i++) {
        TextView textView1 = new TextView(AttendanceActivity.this);
        textView1.setText("" + (daysCounter+1));
        tableRow.addView(textView1, 0);
        //setColor() method sets color according to values
        //arr is an array of attendance values
        switch (setColor(arr[daysCounter])) {
            case "red":
                textView1.setBackgroundColor(getResources().getColor(R.color.red));
                break;
            case "green":
                textView1.setBackgroundColor(getResources().getColor(R.color.green));
                break;
            default:
                textView1.setBackgroundColor(getResources().getColor(R.color.white));
                break;
        }
        daysCounter++;
    }
    tableLayout.addView(tableRow);
}

As seen in the code, the loop is quite straightforward. I am not getting why the numbers are printing as reverse and How to fix them. Please help. Working from long time on this.


Solution

  • you need to change the index of item while adding into tableRow.

    Replace the code

    tableRow.addView(textView1, 0);
    

    to

    tableRow.addView(textView1, i);
    

    I have checked this in the sample app, it is working. let me know it working or not. Thanks