I'm populating TableLayout from Databases. i have created a method buildTable to fetch data from databases and create table rows dynamically, called that method in MainActivity onCreate method. Everything is fine there is no error but it didn't show the Table with data when i run the application.
Here is my try :
private void BuildTable() {
Cursor mCur = testHelper.populateTable();
if (mCur.getCount() != 0) {
if (mCur.moveToFirst()) {
do {
int rows = mCur.getCount();
int cols = mCur.getColumnCount();
// outer for loop
for (int i = 0; i < rows; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 0; j < cols; j++) {
TextView tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tv.setGravity(Gravity.CENTER);
tv.setTextSize(18);
tv.setPadding(0, 5, 0, 5);
tv.setText(mCur.getString(j));
row.addView(tv);
}
t1.addView(row);
}
} while (mCur.moveToNext());
}
}
}
Here is my activity_main
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:weightSum="1">
<ScrollView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/scrollView"
android:background="#6638b4a2">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_table"
android:background="#88f58757"></TableLayout>
</ScrollView>
</LinearLayout>
</ScrollView>
Aucun commentaire:
Enregistrer un commentaire