私のレイアウトは PagerAdapter で膨らみます。両方とも、多数の編集テキスト フィールドがあります。エディット テキスト フィールドの値はオブジェクトに渡され、フラグメントで使用されます。ただし、デバイスを回転させるとすべての値が失われますが、これは正常な動作です。アダプターに設定した値を保存および取得するにはどうすればよいですか?
public class AuthPeemAdapter extends PagerAdapter {
Context mContext;
int[] mResources;
String[] mTitles;
PreferenceAdapter mPreferenceAdapter;
public AuthPeemAdapter.OnAuthPeemInteractionListener mListener;
public interface OnAuthPeemInteractionListener {
void onAuth(String action);
void onRegisterActivationCode(UserInfo userInfo);
void onLogin(UserInfo userInfo);
}
public AuthPeemAdapter(Context context) {
this.mContext = context;
this.mResources = new int[2];
this.mResources[0] = R.layout.shop_login;
this.mResources[1] = R.layout.shop_register;
this.mTitles = new String[2];
this.mTitles[0] = "Login";
this.mTitles[1] = "Register";
this.mPreferenceAdapter = new PreferenceAdapter(context);
}
@Override
public Object instantiateItem(ViewGroup collection, final int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout;
View view;
final EditText username;
final EditText password;
final EditText confirmPassword;
final EditText firstName;
final EditText lastName;
final EditText companyName;
final EditText peemName;
final EditText peemPassword;
Button button;
if (position == 0) {//Login
layout = (ViewGroup) inflater.inflate(mResources[position], collection, false);
view = layout.findViewById(R.id.tv_register_now);
username = (EditText) layout.findViewById(R.id.et_email);
password = (EditText) layout.findViewById(R.id.et_password);
password.setVisibility(View.GONE);
peemName = (EditText) layout.findViewById(R.id.et_peem_name);
peemPassword = (EditText) layout.findViewById(R.id.et_peem_pass);
button = (Button) layout.findViewById(R.id.btn_login);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onAuth(mTitles[position]);
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UserInfo userInfo = new UserInfo();
if (username.getText().toString().isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(username.getText().toString()).matches()) {
username.setError("Please enter valid email address");
return;
} else {
userInfo.setEmail(username.getText().toString());
}
if (peemName.getText().toString().isEmpty()) {
peemName.setError("Cannot be left blank");
return;
} else {
userInfo.setPeemName(peemName.getText().toString());
}
if (peemPassword.getText().toString().isEmpty()) {
peemPassword.setError("Must be at least 6 characters");
return;
} else {
userInfo.setPeemPass(peemPassword.getText().toString());
}
mPreferenceAdapter.serializeUserInfo(userInfo);
mPreferenceAdapter.apply();
mListener.onLogin(userInfo);
}
});
collection.addView(layout);
} else {//Register
layout = (ViewGroup) inflater.inflate(mResources[position], collection, false);
view = layout.findViewById(R.id.tv_register_now);
username = (EditText) layout.findViewById(R.id.et_email);
password = (EditText) layout.findViewById(R.id.et_password);
confirmPassword = (EditText) layout.findViewById(R.id.et_confirm_password);
confirmPassword.setVisibility(View.GONE);
firstName = (EditText) layout.findViewById(R.id.et_first_name);
lastName = (EditText) layout.findViewById(R.id.et_last_name);
companyName = (EditText) layout.findViewById(R.id.et_company_name);
button = (Button) layout.findViewById(R.id.btn_register);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onAuth(mTitles[position]);
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UserInfo userInfo = new UserInfo();
if (username.getText().toString().isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(username.getText().toString()).matches()) {
username.setError("Please enter valid email address");
return;
} else {
userInfo.setEmail(username.getText().toString());
}
if (password.getText().toString().isEmpty()) {
password.setError("Must be at least 6 characters");
return;
} else {
userInfo.setPassword(password.getText().toString());
}
if (companyName.getText().toString().isEmpty()) {
companyName.setError("Cannot be left blank");
return;
} else {
userInfo.setCompany(companyName.getText().toString());
}
if (firstName.getText().toString().isEmpty()) {
firstName.setError("Cannot be left blank");
return;
} else {
userInfo.setFirstName(firstName.getText().toString());
}
if (lastName.getText().toString().isEmpty()) {
lastName.setError("Cannot be left blank");
return;
} else {
userInfo.setLastName(lastName.getText().toString());
}
mListener.onRegisterActivationCode(userInfo);
}
});
collection.addView(layout);
}
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return 2;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
return mTitles[position];
}
public void addOnAuthPeemInteractionListener(AuthPeemAdapter.OnAuthPeemInteractionListener listener) {
mListener = listener;
}
public void removeOnAuthPeemInteractionListener() {
this.mListener = null;
}
}