好吧,我在我的智慧结束。
我在做什么:
- 获取xml
- 解析xml
- 根据解析的xml加载布局(EditText框)(通过带有kay / value东西的包装类)
- 在onSaveInstanceState中保存xml
- 在onSaveInstanceState中保存EditText框内容(获取键/值对)
- - > 手动旋转屏幕
- 加载xml并再次解析它
- 根据新生成的EditText框的键
我想做什么:
- 使用旧内容(取决于键)设置新EditText框的文本
问题
- 未设置文字
代码
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;
}
}
奇怪的是,我可以做这样的事情:
(在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;
}
当我手动点击按钮时它会起作用,但是当我将按钮输入我的Activity(只是将其传递出去)并以编程方式点击它时,它不起作用。
正如你所看到的,我已经尝试了一些无用的东西。我还检查了对象ID并且它们匹配(只是为了排除意外覆盖某处的组件)
我也已经尝试删除所有可见性 - 从一开始就显示EditText-Fields,显然没有解决我的问题。
有人为挑战而奋斗吗? (非常感谢任何帮助)