Reputation: 799
I have done following coding for deriving data's from table and displayed in the android view as a Table columns and rows.
The Database coding is
public List<Country> getAllCountry()
{
List<Country> countryList = new ArrayList<Country>();
//select query
String selectQuery = "SELECT * FROM COUNTRY_LIST";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
{
Country country = new Country();
country.setId(cursor.getString(0));
country.setName(cursor.getString(1));
country.setNationalty(cursor.getString(2));
country.setDate(cursor.getString(3));
// Adding person to list
countryList.add(country);
}
return countryList;
}
The XML is
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab">
<TableRow
>
</TableRow>
</TableLayout>
The Activity Coding is
List<Country> country = db.getAllCountry();
StringBuilder builder = new StringBuilder();
for (Country c: country)
{
builder.append(c.getId()).append(";")
.append(c.getName()).append(";")
.append(c.getNationalty()).append(";")
.append(c.getDate()).append("_");
}
//tv.setText(builder.toString());
builder.toString();
String st = new String(builder);
Log.d("Main",st);
String[] rows = st.split("_");
TableLayout tableLayout = (TableLayout)findViewById(R.id.tab);
tableLayout.removeAllViews();
for(int i=0;i<rows.length;i++){
Log.d("Rows",rows[i]);
String row = rows[i];
TableRow tableRow = new TableRow(getApplicationContext());
tableRow.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
final String[] cols = row.split(";");
Handler handler = null;
for (int j = 0; j < cols.length; j++) {
final String col = cols[j];
TextView columsView = new TextView(getApplicationContext());
columsView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
columsView.setTextColor(color.black);
columsView.setText(String.format("%7s", col));
Log.d("Cols", String.format("%7s", col));
tableRow.addView(columsView);
}
}
The above code not printing anything in the android emulator screen.
Please say what have I done wrong.
Thanks in advance.
Regards, Dinesh
Upvotes: 1
Views: 6364
Reputation: 510
I guess you are missing tableLayout.addView(tableRow) on the last line of the 1st for loop after coming out of 2nd for loop..else row will not be added..
Upvotes: 1
Reputation: 33505
I think you most likely forgot to call this method:
setContentView(<yourLayout>);
that will set layout for your Activity.
Note: Don't forget to test whether your List
is empty or not to be sure you have data.
Upvotes: 0