0

I have this ArrayList:

 ArrayList<Double> debtList = datasource.debtList;
 ArrayList<Double> feeList = datasource.feeList;

How would I print out these two Lists side by side (formatting doesn't matter) in a TableLayout in a loop? Here is layout:

 TableLayout table = (TableLayout) findViewById(R.id.myTableLayout);

3 Answers 3

6

Ok, you have two arraylists debtList and feeList, I assume both the arraylist contains equal number of elements, now iterate through this list, Create Table Row add two textViews to table row, and add tablerow to the tableLayout, so you can do following:

ArrayList<Double> debtList = datasource.debtList;
ArrayList<Double> feeList = datasource.feeList;
TableLayout table = (TableLayout) findViewById(R.id.myTableLayout);
for(int i=0;i<debtList.size();i++)
{
    TableRow row=new TableRow(this);
    double debt = debtList.get(i);
    double fee = feeList.get(i);
    TextView tvDebt=new TextView(this);
    tvDebt.setText(""+debt);
    TextView tvFee=new TextView(this);
    tvFee.setText(""+fee);
    row.addView(tvDebt);
    row.addView(tvFee);
    table.addView(row);
}
Sign up to request clarification or add additional context in comments.

1 Comment

That was perfect thank you. Yes, they were equal by the way.
1

I have found out the solution if the arrayLists is not of equal size.

Here is the logic which i have implemented:

Here is my activity:

public class TestStringActivity extends Activity {
private ArrayList<String> input1 = new ArrayList<String>();
private ArrayList<String> input2 = new ArrayList<String>();
private TableRow row;
private TableLayout inflate;
private TextView txtcol1, txtcol2;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    //Populating the arrayList
    input1.add("1 ");
    input1.add("2 ");
    input1.add("3 ");
    input2.add(" Red");
    input2.add(" Blue");
    input2.add(" Green");
    input2.add(" White");

    inflate = (TableLayout) TestStringActivity.this
            .findViewById(R.id.mytable);

    for (int i = 0, j = 0; i < input1.size() || j < input2.size();) {
        row = new TableRow(TestStringActivity.this);
        txtcol1 = new TextView(TestStringActivity.this);
        if (input1.size() > i) {
            if ((input1.get(i) != null)) {
                txtcol1.setText(input1.get(i));
                i++;
            }
        } else {
            txtcol1.setText("");
        }
        row.addView(txtcol1);

        txtcol2 = new TextView(TestStringActivity.this);
        if ((input2.size() > j)) {
            if (input2.get(j) != null) {
                txtcol2.setText(input2.get(j));
                j++;
            }
        } else {
            txtcol2.setText("");
        }
        this.row.addView(txtcol2);

        inflate.addView(row);

    }

  }
}

Here is my Table Layout main.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mytable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

</TableLayout>

Hope this helps if the arrayLists size are not of equal size.

Comments

0

Assuming the number of entries in the ArrayLists are equal to the number of cells in the TableLayout, you can try something like this:

int index = 0;
TableLayout tableLayout = findViewById(R.id.table_layout);

for(int n = 0, s = tableLayout.getChildCount(); n < s; ++n) {
    double debt = debtList.get(index);
    double fee = feeList.get(index);

    TableRow row = (TableRow) tableLayout.getChildAt(n);
    TextView cell = (TextView) row.findViewById(R.id.textview_cell);

    name.setText("debt = " + debt + ", fee = " + fee);
    index++;
}

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.