我使用Realm在片段1中保存來自EditText的用戶輸入,並在片段2的TextView中顯示該輸入。當用戶單擊第一個片段內的按鈕時,我需要顯示文本里面第二個片段。領域使用的是舊對象,而不是更新
一切似乎都很好,但每次我午餐我的應用程序時,查詢都會在保存對象前被優先處理,舊的輸入顯示在TextView中。
- 當我輸入一些,舊文本已經保存已經和內部的TextView顯示
這裏是節省投入,並OnClick方法裏面片段1方法:
public void setEmail() {
Realm realm = getRealm();
realm.beginTransaction();
final Email emailInput = new Email();
emailInput.setUsername(editEmail.getText().toString());
realm.copyToRealmOrUpdate(emailInput);
realm.commitTransaction();
}
@OnClick(R.id.btn_forgot)
public void onButtonClick() {
setEmail();
}
。 這裏是內部片段2的查詢方法和設置文本:
public void getEmail() {
Realm realm = getRealm();
RealmQuery<Email> queryUser = realm.where(Email.class);
Email resultEmail = queryUser.findFirst();
resendEmailTxt = (AutoResizeTextView) getView().findViewById(R.id.resend_user_email);
if (resendEmailTxt != null) {
this.resendEmailTxt.setText(resultEmail.getUsername());
}
}
@Override
public void onStart() {
super.onStart();
getEmail();
}
模型類:
public class Email extends RealmObject {
@PrimaryKey
public int id = 0;
private String username;
//Getters and setters here
}
如何設置片段?它們是否包含在ViewPager,Activity等中?他們是同時創建的嗎?在我看來他們是,並且在點擊Fragment1的按鈕之前執行Fragment2的'getEmail()'。 –
@RafałZawadzki他們在ViewPager中,我將它們以編程方式添加到pagerAdapter內。我可以在getEmail()之前執行按鈕單擊操作嗎? –
片段位於TabFragment(ViewPager)內部,並且它們是在同一時間創建的。 @RafałZawadzkithx幫助兄弟 –