2

我正在开发一个由片段组成的 android 应用程序。我指developer.android.com的是这个。对于小型设备,我的fragment_container.xml文件被相应的fragment.xml文件替换。

创建的新片段由几个编辑文本框和日期选择器组成。我可以在屏幕方向更改期间以某种方式保存输入到编辑文本框中的数据。
但是日期选择器和列表中的文本会自动重置。

而且,我想保存片段的内容,这样如果用户再次返回同一个片段,他输入的所有数据都必须保留。

我是 android 新手,很长一段时间以来我都陷入了这个问题。请尽快帮我解决这个问题。

我附上代码以供参考。

这是我的MainActivity.java类文件

        ContentFragment newFragment = new ContentFragment();
        Bundle args = new Bundle();
        // Log.d("Inside OnCOntentSelected", String.valueOf(position));
        args.putInt(ContentFragment.ARG_POSITION, position);
        newFragment.setArguments(args);
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();

        // Replace whatever is in the fragment_container view with this
        // fragment,
        // and add the transaction to the back stack so the user can
        // navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();

这是我的ContentFragment.java代码

       import android.os.Bundle;
       import android.support.v4.app.Fragment;
       import android.view.LayoutInflater;
       import android.view.View;
       import android.view.ViewGroup;
       import android.widget.ArrayAdapter;
       import android.widget.Spinner;

public class ContentFragment extends Fragment {

public static final String ARG_POSITION = "position";
int mCurrentPosition = -1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // If activity recreated (such as from screen rotate), restore
    // the previous article selection set by onSaveInstanceState().
    // This is primarily necessary when in the two-pane layout.
    if (savedInstanceState != null) {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);

    }

    // Inflate the layout for this fragment
    return inflater
            .inflate(R.layout.personal_information, container, false);
}

@Override
public void onStart() {
    super.onStart();

    // During startup, check if there are arguments passed to the fragment.
    // onStart is a good place to do this because the layout has already
    // been
    // applied to the fragment at this point so we can safely call the
    // method
    // below that sets the article text.
    // Log.d("Inside OnStart", "Inside OnStart");
    // Log.d("mCurrentPosition OnStart", String.valueOf(mCurrentPosition));
    Bundle args = getArguments();
    if (args != null) {
        // Set article based on argument passed in
        updateContentView(args.getInt(ARG_POSITION));
    } else if (mCurrentPosition != -1) {
        // Set article based on saved instance state defined during
        // onCreateView
        updateContentView(mCurrentPosition);
    }



}

public void updateContentView(int position) {


    mCurrentPosition = position;
    // if (mCurrentPosition == 0) {
    Spinner spinner = (Spinner) getActivity().findViewById(
            R.id.spinnerZodiac);
    // Create an ArrayAdapter using the string array and a default
    // spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this.getActivity(), R.array.zodiac_signs,
            android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    Spinner spinner1 = (Spinner) getActivity().findViewById(
            R.id.spinnerGender);
    // Create an ArrayAdapter using the string array and a default
    // spinner layout
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
            this.getActivity(), R.array.gender,
            android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner1.setAdapter(adapter1);
    // }

}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putInt(ARG_POSITION, mCurrentPosition);

    }
  }
4

0 回答 0