如何遍历二维数组并使结果以 table 形式出现在 android 中

how to loop through 2d array and make the result appear in table form in android

HI 下面是从curson 中获取四列数据并放入二维数组中的代码。基本上有两个问题,一个是我得到最后一个值,因为 nullnullnullnull 意味着所有列都被提取为 null。

秒是我想在多文本行中打印数组,或者如果有任何其他小部件可用,以便我连续获得四个字段。像 id rule_body rule_con 布尔值 0 abc 定义 1 0 一个 0

    c.moveToFirst();
        int i=0;

                    while(c.moveToNext()) {
                    String id = c.getString(c.getColumnIndex("id"));
                    String rb = c.getString(c.getColumnIndex("rule_body"));
                    String cn = c.getString(c.getColumnIndex("rule_cons"));
                    String bl = c.getString(c.getColumnIndex("boole"));
                    table[i][0] = id;
                    table[i][1] = rb;
                    table[i][2] = cn;
                    table[i][3] = bl;
                    ++i;
                }

            for(int a=0;a<count_row;a++)
                for(int b=0;b<count_col;b++) {

                    obj_ml.append(String.valueOf(table[a][b]));
                } 

到目前为止,我在一行中得到了所有结果。任何帮助将不胜感激。

如下更改您的 for 循环

for (int a=0;a<count_row;a++)
{
     for(int b=0;b<count_col;b++) 
      {
          obj_ml.append(String.valueOf(table[a][b]));
      }
     // add to obj_ml new line character '\n'
     obj_ml.append("\n");
}