在Android TableLayout中打印ArrayList内容
我有一个ArrayList:在Android TableLayout中打印ArrayList内容,android,loops,arraylist,tablelayout,Android,Loops,Arraylist,Tablelayout,我有一个ArrayList: ArrayList<Double> debtList = datasource.debtList; ArrayList<Double> feeList = datasource.feeList; 假设ArrayLists中的条目数等于TableLayout中的单元格数,您可以尝试以下操作: int index = 0; TableLayout tableLayout = findViewById(R.id.table_layout);
ArrayList<Double> debtList = datasource.debtList;
ArrayList<Double> feeList = datasource.feeList;
假设
ArrayList
s中的条目数等于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++;
}
int索引=0;
TableLayout TableLayout=findviewbyd(R.id.table\u布局);
对于(int n=0,s=tableLayout.getChildCount();n
好的,您有两个arraylist debtList和feeList,我假设这两个arraylist包含相同数量的元素,现在遍历此列表,创建表行,将两个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);
}
ArrayList debtList=datasource.debtList;
ArrayList feeList=datasource.feeList;
TableLayout table=(TableLayout)findviewbyd(R.id.myTableLayout);
对于(int i=0;i,如果数组列表的大小不相等,我已经找到了解决方案
以下是我实现的逻辑:
以下是我的活动:
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);
}
}
}
公共类TestStringActivity扩展活动{
private ArrayList input1=新的ArrayList();
private ArrayList input2=新的ArrayList();
私人桌排;
私人桌面布局充气;
私有文本视图txtcol1、txtcol2;
/**在首次创建活动时调用*/
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//填充arrayList
输入1.添加(“1”);
输入1.添加(“2”);
输入1.添加(“3”);
输入2.添加(“红色”);
输入2.添加(“蓝色”);
输入2.添加(“绿色”);
输入2.添加(“白色”);
充气=(TableLayout)TestStringActivity.this
.findviewbyd(R.id.mytable);
对于(int i=0,j=0;ii){
if((input1.get(i)!=null)){
txtcol1.setText(input1.get(i));
i++;
}
}否则{
txtcol1.setText(“”);
}
row.addView(txtcol1);
txtcol2=newtextView(TestStringActivity.this);
如果((input2.size()>j)){
if(input2.get(j)!=null){
txtcol2.setText(input2.get(j));
j++;
}
}否则{
txtcol2.setText(“”);
}
this.row.addView(txtcol2);
充气。添加视图(世界其他地区);
}
}
}
以下是我的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>
如果ArrayList的大小不相等,希望这能有所帮助。太好了,谢谢。是的,顺便说一句,它们是相等的。
<?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>