I’m running my code on the emulator with Android 2.2 and it works fine. But when I put it on my device (Galaxy S 2.3.3) the main screen that should show a list of elements in a table – stays blank. Toasts however are shown and also the header with the app_name
The table is made of a button (defined in XML) and a list of elements loaded from a DB.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout android:id="@+id/TableLayoutMain"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TableRow>
<Button android:text="@string/add_button" android:id="@+id/add_button"
android:layout_height="wrap_content" android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" android:layout_width="match_parent"
android:layout_weight="1"></Button>
</TableRow>
</TableLayout>
</ScrollView>
And here the code:
public void showList(Cursor c){
setContentView(R.layout.main);
table = (TableLayout) findViewById(R.id.TableLayoutMain);
do {
TableRow tr = new TableRow(this);
final int id = c.getInt(c.getColumnIndex(DatabaseAdapter.KEY_ROWID));
tr.setId(id);
tr.setBackgroundColor(ROW_COLOR);
tr.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//do stuff
return true;
}
});
tr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do other stuff
}
});
TextView txt = getText(active, id, name);
tr.addView(txt);
table.addView(tr);
} while (c.moveToNext());
c.close();
}
I wasn’t sure if the mixing between XML definitions and programatically adding elements would work. But I have tried various combinations and can say that they both work equally bad. i.e. fine on the emulator and bad on the device.
I have also created a virtual device with android 2.3.3 where the code runs fine as well… so I guess it can’t be the Android version. Any thoughts?
Done, got it to work by using a Relative layout as parent for the TableLayout. Not exactly what I wanted but it works…