如何根据光标向 TableLayout 添加额外的 TableRows?

发布于 2024-10-18 16:00:52 字数 1630 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2024-10-25 16:00:52

嗨试试这个
我认为你每次都添加同一行。所以只需在每次循环迭代时声明它即可。

 TableLayout tl = (TableLayout)findViewById(R.id.table_layout2);

         int x=0;
         for(int i=0;i<c.getCount();i++){
 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);
         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));
         }

Hi try this
i think you adding same row every time. so just declare it every loop iteration.

 TableLayout tl = (TableLayout)findViewById(R.id.table_layout2);

         int x=0;
         for(int i=0;i<c.getCount();i++){
 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);
         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));
         }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文