如何保存片段的EditText数据?

时间:2013-07-15 10:22:47

标签: android android-layout android-fragments

我正在开发一个由片段组成的Android应用程序。我为此提到了developer.android.com。 对于小型设备,我的fragment_container.xml文件将替换为相应的fragment.xml文件。

创建的新片段包含几个edittext框和datepickers左右。 我可以在屏幕方向改变时以某种方式保存输入到edittext框中的数据 但是,datepicker和列表中的文本会自动重置。

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

我是android的新手,很长一段时间我一直坚持这个问题。 请尽快帮我解决这个问题。

我正在附上我的文件代码。

谢谢 Atish Agrawal

这是我的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);

    }
  }

0 个答案:

没有答案
相关问题