I'm trying to inflate my rows dynamically but when I change the orientation of the tablet, I get everything unselected and the checkbox name is filled with the information of the last row. For example, supose I have two checkboxes (two rows) the first one says "hello" and the second says "good bye". If I rotate the tablet I get the first and second checkbox with the name "good bye" and everything unselected.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/tableLayoutList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
I have my row defined like this (mRowLayout.xml):
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayoutRow"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/checkBoxServEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
And then I use the following code to inflate my row:
private void fillTable(View v, Cursor c) {
TableLayout ll = (TableLayout) v.findViewById(R.id.tableLayoutList);
View mTableRow = null;
int i = 0;
while(!c.isAfterLast()){
i++;
mTableRow = (TableRow) View.inflate(getActivity(), R.layout.mRowLayout, null);
CheckBox cb = (CheckBox)mTableRow.findViewById(R.id.checkBoxServEmail);
Log.e("debugging", "email: "+c.getString(c.getColumnIndex(Serv.EMAIL)));
cb.setText( c.getString(c.getColumnIndex(Serv.EMAIL)));
mTableRow.setTag(i);
//add TableRows to TableLayout
ll.addView(mTableRow);
c.moveToNext();
}
}
Does anyone knows why do I have this strange behavior?. Also notice that I put a Log.e for print in my log cat whats going on. And I get "hello" and "good bye" every time I change the orientation of my device, but the setText doesnt seem to work properly.
Please help, I have been with this issue for hours and I don't get it :(
The activity is destroyed and recreated when you change device orientation. You should use the
onSaveInstanceState
callback to save your current UI data. And use onCreate to restore it:
if (savedInstanceState == null) {
// first run
} else {
// not first run
}