我想通过从数据库中提取内容来向表格布局添加行....我正在尝试下面的代码,但它不起作用。 我为数据库相关工作创建了一个“ProductDatabase”类......并在另一个Activity中使用了下面的代码......但它没有显示任何输出
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();
}
}
答案 0 :(得分:0)
要将动态行添加到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.LayoutParams
和TableRow
内的视图使用TableLayout.LayoutParams
。
答案 1 :(得分: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);