Very new to Android. I have an Activity which creates some N custom views. Each custom view it creates gets added to the Activity LinearLayout and shows up on the screen. This works just fine until I rotate the screen. Suddenly the custom views don't display their data correctly. In my test scenario the Activity created 2 instances of my custom view and the first one showed "Name 1" while the second one showed "Name 2" in each custom view's EditText. When I switch the orientation (this is all done on my Nexus 7), Both custom view's EditText say "Name 2". I used println statements in the Activity and in the constructor of the custom view to make sure that the correct values were still being passed in but it just displays differently. Any ideas? Here is some simplified code:
public class EditTemplateWorkout extends Activity{
private int templateWorkoutId = -1;
private TemplateWorkout templateWorkout;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_template);
Intent intent = this.getIntent();
templateWorkoutId = intent.getIntExtra(Messages.TEMPLATEWORKOUTID_MESSAGE, -1);
templateWorkout = templateWorkoutId == -1 ? new TemplateWorkout() : LiftDroidDbHelper.TemplateWorkoutService.get(templateWorkoutId, true, this);
final EditText editTextName = (EditText) this.findViewById(R.id.editTextName);
editTextName.setText(templateWorkout.getName());
editTextName.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(!s.equals("") ){
String txtVal = editTextName.getText().toString();
templateWorkout.setName(txtVal);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
LinearLayout root = (LinearLayout)this.findViewById(R.id.rootLayout);
root.removeAllViews();
for(TemplateExercise te : templateWorkout.getTemplateExercises()){
root = (LinearLayout)this.findViewById(R.id.rootLayout);
System.out.println("activity adding view for "+te.getName());
EditTemplateExerciseView editTemplateExercise = new EditTemplateExerciseView(te, this, null);
editTemplateExercise.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
root.addView(editTemplateExercise);
}
int moot = 3;
}
}
public class EditTemplateExerciseView extends LinearLayout {
TemplateExercise templateExercise;
public EditTemplateExerciseView(TemplateExercise te, Context context, AttributeSet attrs) {
super(context, attrs);
templateExercise = te;
setOrientation(LinearLayout.HORIZONTAL);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_edit_template_exercise, this, true);
TextView txtName = (TextView) this.findViewById(R.id.templateExerciseNameTxt);
txtName.setText(templateExercise.getName() + templateExercise.getTemplateExerciseID());
System.out.println("templateXercise id ="+te.getTemplateExerciseID());
System.out.println("set name:"+templateExercise.getName() + ", confirmed val="+txtName.getText());
}//constructor
}
Here is a copy of my simplified custom view:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/templateExerciseNameTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
</merge>
Here is a link to the Screenshots: http://imgur.com/a/2HqmS
Here is the prinln output results from the code when it renders the good expected view:
01-17 17:44:49.588: I/System.out(22474): activity adding view for Bench Press
01-17 17:44:49.618: I/System.out(22474): templateXercise id =4
01-17 17:44:49.618: I/System.out(22474): set name:Bench Press, confirmed val=Bench Press4
01-17 17:44:49.618: I/System.out(22474): activity adding view for Incline Bench
01-17 17:44:49.648: I/System.out(22474): templateXercise id =6
01-17 17:44:49.648: I/System.out(22474): set name:Incline Bench, confirmed val=Incline Bench6
Here is the println output results from the code when the screen orientation changes and the Activity's onCreate method is called:
01-17 17:45:31.568: I/System.out(22474): activity adding view for Bench Press
01-17 17:45:31.588: I/System.out(22474): templateXercise id =4
01-17 17:45:31.588: I/System.out(22474): set name:Bench Press, confirmed val=Bench Press4
01-17 17:45:31.588: I/System.out(22474): activity adding view for Incline Bench
01-17 17:45:31.618: I/System.out(22474): templateXercise id =6
01-17 17:45:31.618: I/System.out(22474): set name:Incline Bench, confirmed val=Incline Bench6
01-17 17:45:31.648: W/IInputConnectionWrapper(22474): showStatusIcon on inactive InputConnection
01-17 17:45:31.808: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
01-17 17:45:31.878: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
01-17 17:45:31.978: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
01-17 17:45:32.178: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
01-17 17:45:32.318: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
01-17 17:45:32.338: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
You can see that I am passing the expected values into the first custom view but it shows the value of the second one. The code you see here is greatly simplified from what the views originally did. Any android gurus know what I am doing wrong here?