Home > Android > Android: Eclipse and problems with dynamic tables (adding rows)

Android: Eclipse and problems with dynamic tables (adding rows)

January 20th, 2010 mat Leave a comment Go to comments

When using TableLayout with LayoutParams Eclipse will automatically (and wrongly) add the following import:

import android.view.ViewGroup.LayoutParams;

However if you try to use this you will notice that nothing changes. You need to replace the import with the ollowingL

import android.widget.TableRow.LayoutParams;

Now if you rerun your code, you will see that your tables are updating. See below for an example of the code to generate a table

int sizey=10;
int sizex=10;
int btnwidth=24;
TableLayout tl = (TableLayout)findViewById(R.id.MyTableLayout); 

for (int y=0;y<sizey;y++){

// Rows
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

//Cells
for (int x=0;x<sizex;x++){
// Create new cell
	// new button
	Button b = new Button(this);
	b.setText(x+""+y);
	b.setLayoutParams(new LayoutParams(
		LayoutParams.FILL_PARENT,
		LayoutParams.WRAP_CONTENT));
	b.setHeight(btnwidth);
	b.setBackgroundResource(R.drawable.btn_blue);
	b.setOnClickListener(new PegOnClickListener(y,x));
	// add button to row
	tr.addView(b);
	}
// add row to layout
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}


Bookmark and Share
Categories: Android Tags: , ,
  1. nova
    January 20th, 2010 at 22:58 | #1

    This is a general problem with eclipse and auto imports (control+shift+o). I’ve noticed this with automatically importing java.sql.Date rather than java.util.Date also. Just make sure that the imports that eclipse things you need are the imports that you think you need :)

  1. No trackbacks yet.
// unused langs // // // //