为数组中的每个项添加TextView

时间:2022-08-26 14:11:51

I am trying to split sqlite column into an array and then add a TextView for each item of the array. Below is my function to add a TextView for each item in array:

我试图将sqlite列拆分为一个数组,然后为该数组的每个项添加一个TextView。下面是我为数组中的每个项添加TextView的函数:

private void BuildTable() {

    sqlcon.open();
    Cursor c = sqlcon.readEntry();

    int rows = c.getCount();
    int cols = c.getColumnCount();
    String[] array;
    c.moveToFirst();

    // outer for loop
    for (int i = 0; i < rows; i++) {

        TableRow row = new TableRow(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));

        // inner for loop
        for (int j = 0; j < cols; j++) {

            TextView tv = new TextView(this);
            tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
        //    tv.setBackgroundResource(R.drawable.cell_shape);
            tv.setGravity(Gravity.CENTER);
            tv.setTextSize(18);
            tv.setPadding(0, 5, 0, 5);
            array = c.getString(1).split(",");
            for (int k = 0; k < array.length; k++) {
                tv.setText(array[k]);
               row.addView(tv);

           }

        }

        c.moveToNext();

        table_layout.addView(row);

    }
    sqlcon.close();
}

I get the following error:

我收到以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.logquiz.thequiz.logquiz/com.logquiz.thequiz.logquiz.RulesActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

java.lang.RuntimeException:无法启动活动ComponentInfo {com.logquiz.thequiz.logquiz / com.logquiz.thequiz.logquiz.RulesActivity}:java.lang.IllegalStateException:指定的子级已有父级。您必须首先在孩子的父母上调用removeView()。

2 个解决方案

#1


0  

the problem is on the inner for loop

问题出在内部for循环上

for (int k = 0; k < array.length; k++) {
        tv.setText(array[k]);
        row.addView(tv);
}

here you are adding the same TextView multiple to the same row instance, and this is not allowed. Change your code like

在这里,您将相同的TextView多个添加到同一行实例,这是不允许的。改变你的代码就像

for (int k = 0; k < array.length; k++) {
        tv.append(array[k]);
}
row.addView(tv);

this way every TextView is added at most once to the current row

这样,每个TextView最多只添加一次到当前行

#2


0  

I'm guessing the correct solution is even a bit more drastic than Blackbelt suggested. The following snippet actually creates a new TextView for every line of text:

我猜测正确的解决方案甚至比Blackbelt建议的更加激烈。以下代码段实际上为每行文本创建一个新的TextView:

   // inner for loop
    for (int j = 0; j < cols; j++) {

      array = c.getString(1).split(",");
      for (int k = 0; k < array.length; k++) {
        TextView tv = new TextView(this);
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
    //    tv.setBackgroundResource(R.drawable.cell_shape);
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(18);
        tv.setPadding(0, 5, 0, 5);
        tv.setText(array[k]);
        row.addView(tv);

      }
    }

The problem is identified correctly by Blackbelt: you are trying to reuse the same TextView instead of creating a new one each time.

Blackbelt正确识别问题:您尝试重复使用相同的TextView,而不是每次都创建一个新的TextView。

#1


0  

the problem is on the inner for loop

问题出在内部for循环上

for (int k = 0; k < array.length; k++) {
        tv.setText(array[k]);
        row.addView(tv);
}

here you are adding the same TextView multiple to the same row instance, and this is not allowed. Change your code like

在这里,您将相同的TextView多个添加到同一行实例,这是不允许的。改变你的代码就像

for (int k = 0; k < array.length; k++) {
        tv.append(array[k]);
}
row.addView(tv);

this way every TextView is added at most once to the current row

这样,每个TextView最多只添加一次到当前行

#2


0  

I'm guessing the correct solution is even a bit more drastic than Blackbelt suggested. The following snippet actually creates a new TextView for every line of text:

我猜测正确的解决方案甚至比Blackbelt建议的更加激烈。以下代码段实际上为每行文本创建一个新的TextView:

   // inner for loop
    for (int j = 0; j < cols; j++) {

      array = c.getString(1).split(",");
      for (int k = 0; k < array.length; k++) {
        TextView tv = new TextView(this);
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
    //    tv.setBackgroundResource(R.drawable.cell_shape);
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(18);
        tv.setPadding(0, 5, 0, 5);
        tv.setText(array[k]);
        row.addView(tv);

      }
    }

The problem is identified correctly by Blackbelt: you are trying to reuse the same TextView instead of creating a new one each time.

Blackbelt正确识别问题:您尝试重复使用相同的TextView,而不是每次都创建一个新的TextView。