I am inflating a few EditTexts
and adding them to a LinearLayout
:
private void setupCommentViews(){
int i = 0;
Iterator<CommentInformation> iterator = commentInformations.iterator();
while(iterator.hasNext()) {
i++;
View v = LayoutInflater.from(context).inflate(R.layout.comment_row_item, commentsContainer, false);
EditText commentField = (EditText)v.findViewById(R.id.comment);
CommentInformation commentInformation = iterator.next();
final String uniqueId = commentInformation.getUniqueId();
String currentCommentText = comments.get(uniqueId);
if(currentCommentText != null) {
Log.v("into","Setting old text: "+currentCommentText);
commentField.setText(currentCommentText);
} else {
Log.v("into","Setting empty text: "+i);
commentField.setText("Setting empty text: "+i);
}
commentField.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
comments.put(uniqueId, s.toString().trim());
}
});
commentsContainer.addView(v);
}
}
The logs after the first run look like this:
04-01 17:40:41.244: V/into(28770): Setting empty text: 1
04-01 17:40:41.254: V/into(28770): Setting empty text: 2
04-01 17:40:41.254: V/into(28770): Setting empty text: 3
And all of the EditTexts have the correct text in them ("Setting empty text: #")
-
I am adding a TextChangedListener
to my EditText
that I inflate and when the text is changed I update a Map<String, String>
and put the uniqueId as the key and the comment text as the value.
The problem occurs when I rotate the phone after changing one of the comments. I save the comments Map
inside of onSaveInstanceState
:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(COMMENTS, (Serializable)comments);
}
and I then retrieve them inside of onActivityCreated
if(savedInstanceState != null) {
Log.v("into","Retrieving...");
comments = (Map<String, String>)savedInstanceState.getSerializable(COMMENTS);
}
I also iterate through that Map
and verify that there is one entry with the correct uniqueId and correct changed comment text.
-
Next I call setupCommentViews()
again. This time the logs look right, but the EditTexts
ALL have the same text as the final one (the last inflated one).
04-01 17:42:49.664: V/into(28770): Setting empty text: 1
04-01 17:42:49.664: V/into(28770): Setting old text: Changed the second textview
04-01 17:42:49.674: V/into(28770): Setting empty text: 3
But all the EditTexts
now say: "Setting empty text: 3"
Using a ListView
is NOT the solution I'm looking for as there are lots of bugs with EditTexts
inside of ListView
with AdjustResize
softInputMode
-
Can anyone shed any light on what is going on here?