1

我想通过从数据库中获取内容来向表格布局添加行......我正在尝试下面的代码,但它不起作用。我为数据库相关工作创建了一个类“ProductDatabase”......并在另一个活动中使用下面的代码......但它没有显示任何输出

ProductDatabase pData; //Database Class
TableLayout mainTable;
Cursor cr;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_product_database);

    pData = new ProductDatabase(this);
    mainTable = (TableLayout) findViewById(R.id.mainTable);


}

@SuppressWarnings("deprecation")
@Override
protected void onResume() {
    super.onResume();
    pData.open();
    cr=pData.getDatabaseCursor(); //fetching cursor from Database Class
    int index=1;    
        for (cr.moveToFirst(); !cr.isAfterLast(); cr.moveToNext()) {
            TableRow tr=new TableRow(this);
                tr.setId(1000+index);
                tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

                TextView labelName=new TextView(this);
                labelName.setId(2000+index);
                labelName.setText(cr.getString(cr.getColumnIndex(ProductDatabase.KEY_NAME)));
                labelName.setTextColor(Color.BLACK);
                labelName.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                tr.addView(labelName);

                TextView labelBarcode=new TextView(this);
                labelBarcode.setId(3000+index);
                labelBarcode.setText(cr.getString(cr.getColumnIndex(ProductDatabase.KEY_BARCODE)));
                labelBarcode.setTextColor(Color.BLUE);
                labelBarcode.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                tr.addView(labelBarcode);

                TextView labelCost=new TextView(this);
                labelCost.setId(4000+index);
                labelCost.setText(cr.getString(cr.getColumnIndex(ProductDatabase.KEY_COST)));
                labelCost.setTextColor(Color.BLUE);
                labelCost.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                tr.addView(labelCost);

                mainTable.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

                index++;

                            }
    pData.close();

}

}

4

2 回答 2

0
//this layout parem for the alignment of the row
    TableRow.LayoutParams rowParams = 
                          new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 0, 1f);
                TableRow.LayoutParams itemParams = 
                          new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
                                                    TableRow.LayoutParams.MATCH_PARENT, 1f);

                //making the loop for multiple table row
                //start
                for(int i=0;i<tid;i++){
                TableRow tr=new TableRow(this);
                tr.setLayoutParams(rowParams);
                 j++;
                TextView feel_sleep=new TextView(this);
                TextView woke_up=new TextView(this);
                TextView slept_min=new TextView(this);
                //creating the add icon 
                ImageView icon_add=new ImageView(this);
                icon_add.setBackgroundResource(R.drawable.icon_add);

                //creating the delete icon
                ImageView icon_delete=new ImageView(this);
                icon_delete.setBackgroundResource(R.drawable.icon_delete);

                LinearLayout icon_layout=new LinearLayout(this);

                icon_layout.addView(icon_add);
                icon_layout.addView(icon_delete);


                TableRow.LayoutParams params = new TableRow.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    params.gravity = Gravity.CENTER;


                feel_sleep.setLayoutParams(itemParams);
                woke_up.setLayoutParams(itemParams);
                slept_min.setLayoutParams(itemParams);
                icon_layout.setLayoutParams(params);
                icon_layout.setId(j);

                Logger.d("id for i:","**:"+i);      
                Logger.d("id for j:","**:"+j);      

                feel_sleep.setGravity(Gravity.CENTER);
                woke_up.setGravity(Gravity.CENTER);
                slept_min.setGravity(Gravity.CENTER);


                feel_sleep.setText("00:00");
                woke_up.setText("00:00");

                tr.addView(feel_sleep);
                tr.addView(woke_up);
                tr.addView(slept_min);
                tr.addView(icon_layout);
    }

    //this is the tablelayout id `enter code here`
    table_layout.addView(tr);
于 2014-09-18T09:50:51.030 回答
0

要向 a 动态添加行,TableLayout您可以使用以下代码:

    for (int i=0; i < 5; i ++) {
        TableRow tr=new TableRow(this);
        tr.setId(1000 + i);

        TextView labelName=new TextView(this);
        labelName.setId(2000 + i);
        labelName.setText("Test");
        labelName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tr.addView(labelName);

        TextView labelBarcode=new TextView(this);
        labelBarcode.setId(3000+i);
        labelBarcode.setText("Test");
        labelBarcode.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tr.addView(labelBarcode);

        TextView labelCost=new TextView(this);
        labelCost.setId(4000+i);
        labelCost.setText("Test");
        labelCost.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tr.addView(labelCost);

        mainTable.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

    }

我只是稍微更改了您的代码以进行测试。

这里重要的事情是当我影响LayoutParams. 当我添加行时,我使用TableRow.LayoutParamsa 内部的视图TableRow和最后的视图。TableLayout.LayoutParams

于 2013-06-07T18:53:48.620 回答