【问题标题】:How can I add extra TableRows to my TableLayout depending on cursor?如何根据光标向我的 TableLayout 添加额外的 TableRows?
【发布时间】: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


    【解决方案1】:

    你好试试这个 我认为您每次都添加相同的行。所以只需在每次循环迭代时声明它。

     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));
             }
    

    【讨论】:

    • 嗨!完美的。真是烦人,我想不通。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多