如何根据光标向 TableLayout 添加额外的 TableRows?
我的 Android 项目的数据库中有一个结果不同的数据库。我想获取这些结果并在表格布局中显示它们。在表格布局中,我想为每个结果添加一行,并在一个文本视图上写入“日期”列,在第二个文本视图上写入“结果”。两个文本视图位于同一行。我获取数据并将其存储在游标中。
我的问题是:如何使用游标并根据数据库中有多少数据添加一行?
现在我尝试为游标中的每一行添加一行,如下所示:
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.
但我不想删除它,我想添加更多。有什么想法吗?谢谢!
I have a database with different results in my database in my Android project. I want to fetch these results and show them in a tablelayout. In the tablelayout I want to add a row for each result, and write the column "date" on one textview, and the "result" on the second textview. Both textviews are on the same row. I fetch the data and store it in a cursor.
My question is: How do I use the cursor and add a row depending on how much data there is in my database?
Right now I try to add a row for each row in the cursor like this:
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));
}
But I think I need to create a NEW tablerow, cause I get this error message:
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.
But I don't want to remove it, I want to add more. Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗨试试这个
我认为你每次都添加同一行。所以只需在每次循环迭代时声明它即可。
Hi try this
i think you adding same row every time. so just declare it every loop iteration.