问题描述:

Alright, I'm at my wits end.

What I am doing:

- GET an xml

- Parse xml

- Load layouts (EditText boxes) depending on the parsed-xml (through a wrapper class with kay/value stuff)

- saving the xml in onSaveInstanceState

- saving the EditText boxes content (getting the key/value-pairs) in onSaveInstanceState

- --> rotating the screen manually

- loading the xml and parsing it again

- getting the key/value pairs of the EditText boxes depending on the keys of the newly generated EditText boxes

What I am trying to do:

- setting the text of the new EditText boxes with the old content (depending on the keys)

Problem

- the text is not set

Code

MyActivity.class

 private String commentRequestResult = "";

private CommentSectionComponent commentSectionComponent;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

...

Intent intent = getIntent();

publicationId = intent.getStringExtra(ExtraKeyProvider.PUBLICATION_ID);

...

try {

if (savedInstanceState != null && ActivityStackHelper.isCalledInSuccession(this)) {

...

commentRequestResult = savedInstanceState.getString(BundleKeyProvider.PUBLICATIONS_DETAIL_ACTIVITY_COMMENTS);

createCommentsSection(commentRequestResult, savedInstanceState);

} else {

...

}

} catch (Exception e) {

Message.show(e);

}

}

private void createCommentsSection(String internalRequestResult, Bundle savedInstanceState) {

XmlParser parser = new XmlParser(internalRequestResult);

commentSectionComponent = new CommentSectionComponent(this);

BaseCommentComponent newCommentComponent = getNewCommentComponent();

commentSectionComponent.addCommentComponent(newCommentComponent);

...

fillCommentSectionFromSavedInstance(savedInstanceState);

}

private void fillCommentSectionFromSavedInstance(Bundle savedInstanceState) {

if (savedInstanceState != null) {

System.out.println("restoring...");

String lastSelectedKey = BundleKeyProvider.PUBLICATIONS_DETAIL_ACTIVITY_COMMENTS_LAST_FOCUS;

String lastSelected = savedInstanceState.getString(lastSelectedKey);

List<BaseCommentComponent> baseCommentComponents = commentSectionComponent.getBaseCommentComponents();

for (BaseCommentComponent baseCommentComponent : baseCommentComponents) {

Comment comment = baseCommentComponent.getComment();

baseCommentComponent.openCommentForWriting();

baseCommentComponent.setText("DOES NOT WORK");

//getting the EditText from baseCommentComponent does also not help

}

}

}

BaseCommentComponent

public abstract class BaseCommentComponent {

protected LinearLayout currentLinearLayout;

protected Context context;

protected Request request;

private Button sendButton;

protected Button abortButton;

private final EditText textField;

private final EditText nameTextField;

private Comment comment;

public BaseCommentComponent(LinearLayout currentLinearLayout, Comment comment, Context context, Request request, Button sendButton, Button abortButton, EditText textField,

EditText nameTextField) {

this.currentLinearLayout = currentLinearLayout;

this.comment = comment;

this.context = context;

this.request = request;

this.sendButton = sendButton;

this.abortButton = abortButton;

this.textField = textField;

this.nameTextField = nameTextField;

OnClickListener abortListener = getAbortButtonListener(sendButton, abortButton, textField, nameTextField);

this.abortButton.setOnClickListener(abortListener);

OnClickListener replyButtonClickedListener = getInitialButtonListener();

this.sendButton.setOnClickListener(replyButtonClickedListener);

}

private OnClickListener getInitialButtonListener() {

System.out.println("getInitialButtonListener");

OnClickListener replyButtonClickedListener = new OnClickListener() {

@Override

public void onClick(View v) {

openCommentForWriting();

sendButton.setOnClickListener(getSendButtonClickedListener());

}

};

return replyButtonClickedListener;

}

public void openCommentForWriting() {

System.out.println("openCommentForWriting");

textField.setVisibility(EditText.VISIBLE);

nameTextField.setVisibility(EditText.VISIBLE);

abortButton.setVisibility(Button.VISIBLE);

sendButton.setText("Send");

}

private OnClickListener getAbortButtonListener(final Button replyButton, final Button replyButtonAbort, final EditText replyTextField, final EditText replyNameTextField) {

System.out.println("getAbortButtonListener");

OnClickListener abortListener = new OnClickListener() {

@Override

public void onClick(View v) {

restoreOriginalCommentComponentState();

}

};

return abortListener;

}

private void restoreOriginalCommentComponentState() {

System.out.println("restoreOriginalCommentComponentState");

sendButton.setText(getOriginalButtonText());

OnClickListener initialReplyButtonListener = getInitialButtonListener();

sendButton.setOnClickListener(initialReplyButtonListener);

abortButton.setVisibility(Button.GONE);

textField.setText("");

textField.setVisibility(EditText.GONE);

nameTextField.setText("");

nameTextField.setVisibility(EditText.GONE);

}

private OnClickListener getSendButtonClickedListener() {

...

return sendCommentListener;

}

private Comment buildComment(final EditText replyTextField, final EditText replyNameTextField, final String commentID) {

String commentText = replyTextField.getText().toString();

Editable name = replyNameTextField.getText();

Comment comment = new Comment(commentID);

comment.setAuthor(name.toString());

comment.setText(commentText);

return comment;

}

protected abstract String getOriginalButtonText();

protected abstract String getPartialUrl();

protected abstract String getMessageToBeDisplayedOnSuccessfulSending();

public LinearLayout getLinearLayout() {

return currentLinearLayout;

}

public Button getSendButton() {

return sendButton;

}

public void setText(final String text) {

// ((Activity) context).runOnUiThread(new Runnable() {

//

// @Override

// public void run() {

textField.setText(text);

// }

// });

//This did not fix it

}

public void setName(String name) {

System.out.println("setting text: " + name);

nameTextField.setText(name);

}

public Comment getComment() {

return comment;

}

public EditText getTextField() {

return textField;

}

public EditText getNameTextField() {

return nameTextField;

}

}

CommentCreationComponent

public class CommentCreationComponent extends BaseCommentComponent {

public CommentCreationComponent(LinearLayout currentLinearLayout, Comment comment, Context context, Request request, Button sendButton,

Button abortButton, EditText textField, EditText nameTextField) {

super(currentLinearLayout, comment, context, request, sendButton, abortButton, textField, nameTextField);

sendButton.setVisibility(Button.VISIBLE);

sendButton.setText(getOriginalButtonText());

}

@Override

protected String getOriginalButtonText() {

return "New comment";

}

@Override

protected String getPartialUrl() {

return "publication/addComment";

}

@Override

protected String getMessageToBeDisplayedOnSuccessfulSending() {

return "Comment was sent";

}

}

CommentSectionComponent

public class CommentSectionComponent {

private LinearLayout placeHolder;

private List<BaseCommentComponent> baseCommentComponents = new ArrayList<BaseCommentComponent>();

public CommentSectionComponent(Context context) {

Activity activity = (Activity) context;

placeHolder = (LinearLayout) activity.findViewById(R.id.placeHolder);

TextView commentSectionHeader = new TextView(context);

commentSectionHeader.setText("Kommentare");

ComponentHelper.convertToHeaderLabel(commentSectionHeader, context);

ComponentHelper.setPublicationDetailLayout(placeHolder);

placeHolder.addView(commentSectionHeader);

}

public void addCommentComponent(BaseCommentComponent comment) {

baseCommentComponents.add(comment);

LinearLayout linearLayout = comment.getLinearLayout();

placeHolder.addView(linearLayout);

}

public List<BaseCommentComponent> getBaseCommentComponents() {

return baseCommentComponents;

}

}

The strange thing is, that I can do something like this:

(in BaseCommentComponent)

private OnClickListener getInitialButtonListener() {

System.out.println("getInitialButtonListener");

OnClickListener replyButtonClickedListener = new OnClickListener() {

@Override

public void onClick(View v) {

openCommentForWriting();

sendButton.setOnClickListener(getSendButtonClickedListener());

textField.setText("yep");

}

};

return replyButtonClickedListener;

}

and it works when i click the button manually, but it doesn't work when i get the button into my Activity (just passing it up) and click it programmatically.

As you can see, i already tried some stuff without avail. I also checked the object-id's and they match (just to exclude accidentally overwriting the component somewhere)

I also already tried removing all the visibility-stuff to show the EditText-Fields from the get-go, obviously did not fix my problem.

Someone up for the challange? (Any help would be much appreciated)

相关阅读:
Top