简体   繁体   English

如何在Android中创建具有动态行数的表

[英]How to create table with dynamic number of rows in Android

I am trying to fill a TableLayout with a dynamic amount of rows based on an Array of icons, however i cannot get the rows to display on screen. 我正在尝试使用基于图标数组的动态行填充TableLayout,但是我无法使行显示在屏幕上。 The table should display a 5x3 grid of icons. 表格应显示5x3的图标网格。 The code I am using is below: 我正在使用的代码如下:

    Services serv = new Services();

    Item[] iconArray = serv.getIcons();

    TableLayout table = (TableLayout) findViewById(R.id.table1);

    int rowNum = iconArray.length/5;
    TableRow rows[]= new TableRow[rowNum];

    int count = 0;
    for (int i = 0; i < iconArray.length; i++) { 
        final String name = iconArray[i].name; 
        final int icon = iconArray[i].icon;

        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        View v = inflater.inflate(R.layout.icon, null,false);
        TextView text = (TextView) v.findViewById(R.id.text);
        text.setText(name);

        ImageView imageicon = (ImageView) v.findViewById(R.id.icon);
        imageicon.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Do some stuff
            }
        });
        Drawable drawicon = getResources().getDrawable(icon);
        imageicon.setBackgroundDrawable(drawicon);

        if(i == 4) //End of Row
        {   
            count++;
        }
        if(i == 9) //End of Row2
        {   
            count++;
        }

        rows[count] = new TableRow(this);
        rows[count].addView(v);
        table.addView(rows[count]);
    }

This does not output anything to the screen and the TableLayout remains empty. 这不会向屏幕输出任何内容,并且TableLayout保持为空。 Can anyone tell me if I'm on the right lines? 谁能告诉我我在正确的路线上吗?

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

First of all, i will make a specific layout for my row : 首先,我将为我的行做一个特定的布局:

my_row.xml my_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:weightSum="1" >

    <ImageView
        android:id="@+id/myImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"" />

</TableRow>

Then, i will simply fill my table with my rows in a loop : 然后,我将循环中的行简单地填入表中:

static TableRow row;
Item[] iconArray = serv.getIcons();
TableLayout table = (TableLayout) myView.findViewById(R.id.table1);

for (int i = 0; i < iconArray.length; i++) {

    LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
    row = (TableRow) inflater.inflate(R.layout.my_row, cont, false);

    final String name = iconArray[i].name; 
    final int icon = iconArray[i].icon;

    TextView text = (TextView) v.findViewById(R.id.myText);
    text.setText(name);

    ImageView imageicon = (ImageView) v.findViewById(R.id.myImg);
    imageicon.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //Do some stuff
        }
    });
    Drawable drawicon = getResources().getDrawable(icon);
    imageicon.setBackgroundDrawable(drawicon);

    table.addView(row);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM