1

我的布局在 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;
}

}

4

1 回答 1

1

尝试这个:

onSaveInstanceState在您的片段中实现该方法。

    public void onSaveInstanceState(Bundle savedState) {

    super.onSaveInstanceState(savedState);

    // getValues() is a method in your Adapter return your editext values..
    String[] values = mAdapter.getValues(); 
    savedState.putStringArray("myKey", values);

}

现在,当重新创建设备旋转fragment时,您可以在片段onCreate方法中检索数据

    public void onCreate (Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        String[] values = savedInstanceState.getStringArray("myKey");
        if (values != null) {
           mAdapter = new MyAdapter(values); //pass values to your adapter class..
        }
    }

}
于 2017-01-10T11:09:35.093 回答