Archiv nach Schlagworten: Class

Android :: create dynamic table programmatically

My problem was to create a dynamic table and not hard xml-coded. So I’ve wrote a table class to create it programmatically. The created table has also borders.

TableGenerator class

public class TableGenerator {
    private final Context mContext;
    private TableLayout mTable;

    private TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams();
    private TableRow.LayoutParams colParams = new TableRow.LayoutParams();

    public TableGenerator(Context context) {
        mContext = context;
        mTable = new TableLayout(context);
        rowParams.setMargins(0, 0, 0, 1);
        colParams.setMargins(0, 0, 1, 0);

        TableLayout.LayoutParams lptable = new TableLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mTable.setLayoutParams(lptable);

        mTable.setStretchAllColumns(true);
        mTable.setBackgroundColor(mContext.getResources().getColor(
                R.color.table_background));
    }

    public void addRow(String[] data) {
        TableRow tr = new TableRow(mContext);
        tr.setBackgroundColor(mContext.getResources().getColor(
                R.color.table_background));

        tr.setLayoutParams(rowParams);

        for (int iCol = 0; iCol < data.length; iCol++) {
            TextView tvCol = new TextView(mContext);
            tvCol.setText(data[iCol]);
            tvCol.setGravity(Gravity.CENTER | Gravity.CENTER);
            tvCol.setPadding(3, 3, 3, 3);
            tvCol.setTextColor(mContext.getResources().getColor(
                    R.color.text_black));
            tvCol.setLayoutParams(colParams);
            tvCol.setBackgroundColor(mContext.getResources().getColor(
                R.color.row_background));
            tr.addView(tvCol);
        }

        mTable.addView(tr);
    }

    public TableLayout getTable() {
        return mTable;
    }
}

how to use the class:

MainActivity class

public class MainActivity extends Activity {
	private ScrollView layMain;
	private TableGenerator mTable;

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

		showTable();
	}

	private void showTable() {
		mTable = new TableGenerator(getApplicationContext());
		layMain = (ScrollView)findViewById(R.id.table);

		String[] firstRow = {"col1", "col2", "col3", "col4"};
		String[] secondRow = {"11", "12", "13", "14"};
		String[] thridRow = {"2a", "2b", "2c", "2d"};

		mTable.addRow(firstRow);
		mTable.addRow(secondRow);
		mTable.addRow(thridRow);

		layMain.removeAllViews();
		layMain.addView(mTable.getTable());
	}
}

to successfully use the code above you need to set the main.xml in layout folder to

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/table"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ScrollView>

</LinearLayout>

now we need some color and string definitions res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="app_name">Table</string>
	<color name="table_background">#999999</color>
	<color name="row_background">#ffffff</color>
</resources>

Downloads

Table.zip (Project files)
Table-180x300 in

VN:F [1.9.22_1171]
Rating: 4.8/5 (4 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 2 votes)

Javascript :: table-Klasse

meine erste selbst geschriebene Javascript-Klasse.

function Table(id) {
	this.rows = 0;
	this.cols = 0;
	if(id != null)
		this.table = document.getElementById(id);
}

Table.prototype = {
	createTable: function(parentElement,id,rows,cols,contentArray) {
		this.table = document.createElement("table");
		this.table.setAttribute("id",id);
		document.getElementById(parentElement).appendChild(this.table);

		for(var i_rows = 0; i_rows < rows; i_rows ++) {
			this.addRow(contentArray[i_rows*cols]);
		}

		var colArray = new Array();
		for(var i_cols = 1; i_cols < cols; i_cols ++) {
			for(var i_rows = 0; i_rows < rows; i_rows ++) {
				colArray[i_rows] = contentArray[i_rows*cols+i_cols];
			}
			this.addCol(colArray);
		}
	},

	addRow: function(content) {
		var newrow = document.createElement("tr");
		var existingRows = this.table.getElementsByTagName("tbody");

		if(this.rows == 0)
		{
			var newTbody = document.createElement("tbody");
			newTbody.appendChild(newrow);
			this.table.appendChild(newTbody);
			existingRows = this.table.getElementsByTagName("tbody");
			if(this.cols == 0)
				this.cols ++;
		}

		for(var i = 0; i < this.cols; i++) {
			newrow.appendChild(this.createNewCell(content[i]));
		}
		existingRows[0].appendChild(newrow);
		this.rows ++;
	},

	addCol: function(content) {
		if(this.rows == 0)
			this.addRow(content);
		else {
			var existingRows = this.table.getElementsByTagName("tr");
			for(var i = 0; i < this.rows; i++) {
				existingRows[i].appendChild(this.createNewCell(content[i]));
			}
			this.cols ++;
		}

	},

	setClass: function(css_class) {
		this.table.className = css_class;
	},

	createNewCell: function(content) {
		var newcell = document.createElement("td");
		var newcontent = document.createTextNode(content);

		newcell.appendChild(newcontent);
		return newcell;
	}
}

diese Klasse kann wie folgt aufgerufen werden

//hier muss schon folgendes im HTML-Code existieren:var table = new Table('mytable');
table.addRow('1');
table.addCol('2');
table.addCol('3');
table.addRow(new Array('4','5','6'));
table.addCol(new Array('7','8'));
table.addRow(new Array('9','10','11','12'));
table.setClass('border');

//oder so, wenn noch nichts exisitert außer z.B.
var table1 = new Table();
content = new Array('1','2','3','4',
	'5','6','7','8',
	'9','10','11','12');
table1.createTable('parent','table1',3,4,content);
table1.setClass('border');

Viel Spaß damit!

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)