我有这个ArrayList:
ArrayList<Double> debtList = datasource.debtList;
ArrayList<Double> feeList = datasource.feeList;
如何在循环中的TableLayout中并排打印这两个列表(格式无关紧要)?这是布局:
TableLayout table = (TableLayout) findViewById(R.id.myTableLayout);
答案 0 :(得分:6)
好的,你有两个arraylists debtList和feeList,我假设两个arraylist包含相同数量的元素,现在遍历这个列表,Create Table Row将两个textView添加到表行,并将tablerow添加到tableLayout,这样你就可以了做以下事情:
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);
}
答案 1 :(得分:0)
假设ArrayList
中的条目数等于TableLayout
中的单元格数,您可以尝试这样的事情:
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++;
}
答案 2 :(得分:0)
如果 arrayLists的大小不同,我找到了解决方案。
这是我实施的逻辑:
这是我的活动:
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);
}
}
}
这是我的表格布局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>
如果arrayLists的大小不相同,希望这会有所帮助。