存储一个片段中的值并在另一个片段中检索

时间:2014-06-13 09:47:37

标签: android android-fragments

我有3个名为Personal_info,contact_info和professional_info的片段。当我的应用程序启动时,默认显示个人信息。在personal_info中,用户填写所有信息,当他点击“继续”按钮时,应显示下一个contact_info片段。在contact_info中,用户填写所有详细信息,当他点击继续时,professional_info应该打开。现在,在最后一个为profession_info的片段中,我调用了一个将所有信息发送到服务器的Web服务。我的问题是

1)如何从personal_info调用contact_info,因为它们都是片段。

2)如何将personal_info frag和contact_info frag中的值转换为professional_info。

MainActivity.java

public class MainActivity extends MainFragment {

    private FrameLayout container;
    private TextView headerid;
    FragmentManager manager;
    FragmentTransaction transaction;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.main);
        initialize ();

    }


    private void initialize() {

        container = (FrameLayout) findViewById (R.id.container);
        headerid = (TextView) findViewById (R.id.header_id);
//        manager=getFragmentManager ();
//        transaction=manager.beginTransaction ();
//        PersonalInfo info=new PersonalInfo ();
//        transaction.add (R.id.container,info,"personal_info");
//        transaction.commit ();
        MainFragment.startFragment (new PersonalInfo (),true);
    }
}

Personal_info.Java

public class PersonalInfo extends Fragment implements View.OnClickListener {
    View view;
    //    private EditText etfirstname;
    private EditText etlastname;
    private Spinner spgender;
    private Spinner spage;
    private Spinner spnationality;
    private Spinner sprelegion;
    private Button btcontinue1;
    private LinearLayout personalinfo;
    private static String first_name, last_name, gender, age, nationality, religion;
    private String[] gender_array;
    private ArrayAdapter<String> gender_str;
    private Context c = getActivity ();
    private String[] religion_array;
    private ArrayAdapter<String> religion_str;
    private String[] state_array;
    private ArrayAdapter<String> state_str;
    private String[] age_array;
    private ArrayAdapter<String> age_str;
    private String[] nationality_array;
    private ArrayAdapter<String> nationality_str;
    private String str_gender;
    private String[] gender_code;
    private String str_religion, gender_id, religion_id;
    private String[] religion_code;
    private String str_age;
    private String age_substring;
    private String str_nationality;
    private String[] nationality_code;
    private String nationality_id;
    private Button btcontinue1;
    private Spinner spgender;
    private EditText etfirstname;
    private EditText etlastname;
    private Spinner spage;
    private Spinner spnationality;
    private Spinner sprelegion;
    TextView header;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate (R.layout.personal_info_frag, container, false);
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        initialize ();
//        spinnerSetup ();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated (savedInstanceState);

    }

    private void spinnerSetup() {


//        gender_array = getResources ().getStringArray (R.array.Gender);
//        Arrays.sort (gender_array, 1, gender_array.length);
//        //gender_str = new ArrayAdapter<String> (c, R.layout.textview_spinner, gender_array);
//        gender_str.setDropDownViewResource (android.R.layout.simple_dropdown_item_1line);
//        spgender.setAdapter (gender_str);

        religion_array = getResources ().getStringArray (R.array.Religion);
        Arrays.sort (religion_array, 1, religion_array.length);
        religion_str = new ArrayAdapter<String> (c, R.layout.textview_spinner, religion_array);
        religion_str.setDropDownViewResource (android.R.layout.simple_dropdown_item_1line);
        sprelegion.setAdapter (religion_str);

        age_array = getResources ().getStringArray (R.array.Age);
        Arrays.sort (age_array, 1, age_array.length);
        age_str = new ArrayAdapter<String> (c, R.layout.textview_spinner, age_array);
        age_str.setDropDownViewResource (android.R.layout.simple_dropdown_item_1line);
        spage.setAdapter (age_str);

        nationality_array = getResources ().getStringArray (R.array.Nationality);
        nationality_str = new ArrayAdapter<String> (c, R.layout.textview_spinner, nationality_array);
        nationality_str.setDropDownViewResource (android.R.layout.simple_dropdown_item_1line);
        spnationality.setAdapter (nationality_str);


    }

//    private void initialize() {
//
//        etfirstname = (EditText) view.findViewById (R.id.et_first_name);
//        etlastname = (EditText) view.findViewById (R.id.et_last_name);
//        spgender = (Spinner) view.findViewById (R.id.sp_gender);
//        spage = (Spinner) view.findViewById (R.id.sp_age);
//        spnationality = (Spinner) view.findViewById (R.id.sp_nationality);
//        sprelegion = (Spinner)view.findViewById (R.id.sp_relegion);
//        btcontinue1 = (Button) view.findViewById (R.id.bt_continue_1);
//        btcontinue1.setOnClickListener (this);
//    }

    @Override
    public void onClick(View view) {

        first_name = etfirstname.getText ().toString ();
        last_name = etlastname.getText ().toString ();

        str_gender = (String) spgender.getSelectedItem ();//gender value
        gender_code = getResources ().getStringArray (R.array.Gender_id);
        gender_id = gender_code[spgender.getSelectedItemPosition ()];// gender code


        //for religion
        str_religion = (String) sprelegion.getSelectedItem ();
        religion_code = getResources ().getStringArray (R.array.Religion_Code);
        religion_id = religion_code[sprelegion.getSelectedItemPosition ()];

        str_age = (String) spage.getSelectedItem ();
        age_substring = str_age.substring (0, 2).trim ();

        //for nationality
        str_nationality = (String) spnationality.getSelectedItem ();
        nationality_code = getResources ().getStringArray (R.array.Nationality_code);
        nationality_id = nationality_code[spnationality.getSelectedItemPosition ()];




    }

MainFragment.Java

public class MainFragment extends Activity {

    Context mContext;
    public static FragmentManager fManager;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initialize();



    }


    private void initialize() {
        mContext = getApplicationContext();
        fManager = getFragmentManager ();
    }

    public static void startFragment(Fragment fragment, boolean isToBackStack) {

        /*
         * Restricting Fragment transition if same fragment available in top of
         * BackStack.
         *
         * If tempFragment is Null it means there is no fragment was added yet.
         */

        Fragment tempFragment = fManager.findFragmentById(R.id.container);
        if (tempFragment != null && tempFragment.getClass().getName().equals(fragment.getClass().getName())) {
            return;
        }

        /*
         * Actual transition of Fragment
         */

        FragmentTransaction fragt = fManager.beginTransaction();
//        fragt.setCustomAnimations(android.R.anim.slide_out_right,android.R.anim.slide_in_left);
        fragt.replace(R.id.container, fragment);

        // hide background fragment
        fragt.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        if (isToBackStack)
            fragt.addToBackStack(fragment.getClass().getName());
        fragt.commit();

    }
}

1 个答案:

答案 0 :(得分:1)

就个人而言,我更愿意让创建片段的活动成为促进沟通的活动。

如果我们有一个活动A,我让它实现一个可以被片段使用的接口。当片段完成并想要发送数据时,它会调用接口上的活动。然后是活动打开下一个片段并为其提供所需的值。

可能是这样的:

interface InformationInterface {
   public void informationGathered(Map<String,String> personalInformation);
}

public class MainActivity extends Activity implements InformationInterface {
   ...lifeCycleMethods...

   public void informationGathered(Map<String,String> personalInformation) {
      //Start the next fragment, giving it the personalInformation
   }
}

//Then in the fragment, we get the interface of the activity
public class PersonalInfoFragment extends Fragment {
   private InformationInterface callback;

   ...lifecycleMethods...

   public void onAttach(Activity activity) {
    super.onAttach(activity);

    if (activity instanceof InformationInterface) {
        this.callback = (InformationInterface)activity;
    } else {
        throw new IllegalArgumentException("Activity doesn't have InformationInterface");
    }
}

private void next() {
   //Called when user presses next
   callback.informationGathered(...map with required values...);
}

这种方式接口不需要彼此了解,活动负责它们之间的所有通信。

http://developer.android.com/guide/components/fragments.html