【发布时间】:2011-02-22 16:34:59
【问题描述】:
我在我的 Android 项目中的数据库中有一个不同结果的数据库。我想获取这些结果并在表格布局中显示它们。在表格布局中,我想为每个结果添加一行,并在一个 textview 上写入“date”列,在第二个 textview 上写入“result”。两个文本视图都在同一行。我获取数据并将其存储在游标中。
我的问题是:如何使用游标并根据数据库中有多少数据添加一行?
现在我尝试为光标中的每一行添加一行,如下所示:
TableLayout tl = (TableLayout)findViewById(R.id.table_layout2);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView mTvDate = new TextView(this);
TextView mTvResult = new TextView(this);
int x=0;
for(int i=0;i<c.getCount();i++){
mTvDate.setText(date[x]);
mTvResult.setText(""+nocorrect[x]+" av "+ noqs[x] +" ("+ proc[x]+ "%)");
mTvDate.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
mTvResult.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(mTvDate);
tr.addView(mTvResult);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
但我认为我需要创建一个新的表行,因为我收到以下错误消息:
02-22 09:22:17.543: ERROR/AndroidRuntime(333): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
但我不想删除它,我想添加更多。有任何想法吗?谢谢!
【问题讨论】:
标签: android cursor tablelayout