我有线性布局,我以编程方式添加tablelayout和行。
当我第一次调用方法时,它工作正常,数据显示给定的行数。
在下一次调用时,前一行仍然可见,并且下面将新数据添加到以前的数据中。我试图使用所有删除视图的方法,但仍然无法摆脱旧行。请帮助。
我的表格代码:
private void maketripsumtable(String pname) {
if (((LinearLayout) sumtablelay).getChildCount() > 0) {
((LinearLayout) sumtablelay).removeAllViews();
Log.i(TAG, "Views " + sumtablelay.getChildCount() + tableLayout1.getChildCount());
tableLayout1.removeAllViewsInLayout();
Log.i(TAG, "Pending Views " + tableLayout1.getChildCount());
}
//Create Table Layout and set its default attributes
tableLayout1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
//add Tablelayout to HScroll
sumtablelay.addView(tableLayout1);
//Create Table row and set its default attribute
TableRow tableRow1 = new TableRow(this);
tableRow1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
//tableRow1.setBackground(new ColorDrawable(tablecolor1));
//add row to tablelayout
Log.i(TAG,"Views at Rows "+tableRow1.getChildCount());
tableRow1.removeAllViews();
Log.i(TAG,"Pending Views at Rows "+tableRow1.getChildCount());
tableLayout1.addView(tableRow1);
//add headings in table row
textView = new TextView(this);
textView.setText("Date");
textView.setPadding(30, 2, 2, 2);
textView.setAllCaps(true);
// textView.setTextColor(textcolorpri);
textView.setTextSize(15);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
tableRow1.addView(textView);
textView = new TextView(this);
textView.setText("Category");
textView.setPadding(20, 2, 2, 2);
textView.setAllCaps(true);
// textView.setTextColor(textcolorpri);
textView.setTextSize(15);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
tableRow1.addView(textView);
tableRow1.addView(textView);
//Get Data from Table
Cursor getexpdata = databaseWorking.getalldatatswithcons(PerformanceTweaks.expdata, "pName", pname);
while (getexpdata != null && getexpdata.moveToNext()) {
DataEncap DataEncap = new DataEncap();
DataEncap.setExpddate(getexpdata.getString(getexpdata.getColumnIndex("dated")));
DataEncap.setExpcat(getexpdata.getString(getexpdata.getColumnIndex("cate")));
tempedata.add(DataEncap);
}
//Adding Data to above layout table.
for (int a = 0; a < tempedata.size(); a++) {
tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
textView = new TextView(this);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
textView.setText(tempedata.get(a).getExpddate());
// textView.setWidth(col1);
textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
// textView.setTextColor(textcolorpri);
textView.setPadding(10, 0, 0, 0);
tableRow.addView(textView);
textView = new TextView(this);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
textView.setText(tempedata.get(a).getExpcat());
// textView.setWidth(col1);
textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
// textView.setTextColor(textcolorpri);
textView.setPadding(10, 0, 0, 0);
tableRow.addView(textView);
tableLayout1.addView(tableRow, new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
}
tablelay.setVisibility(View.VISIBLE);
}
答案 0 :(得分:0)
您可以删除以下所有视图使用方法 sumtablelay.removeAllViews();