【问题标题】:How to get value from one fragment and pass to another fragment如何从一个片段中获取价值并传递给另一个片段
【发布时间】:2015-08-13 10:44:47
【问题描述】:

我有两个片段,一个是片段 X 和片段 y。片段 X 用于使用 web 服务进行注册过程。我想将相同的值从 Fragment X 传递到 Fragment Y。

这是我的代码:

片段 x:

Signup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String sName = Name.getText().toString();
                    String sPhoneNo = Phone.getText().toString();
                    String sEmailId = EmailId.getText().toString();



                    // Instantiate Http Request Param Object
                    RequestParams params = new RequestParams();

                    if (Utility.isNotNull(sName) && Utility.isNotNull(sPhoneNo) && Utility.isNotNull(sEmailId) ) {
                        if (Utility.line2_validate(sName)) {
                            if (Utility.validate(sPhoneNo)) {
                                if (Utility.email_validate(sEmailId)) {

                                   //Get value from this Fragment 
                                    FragmentY pf = new FragmentY ();
                                    Bundle args = new Bundle();
                                    args.putString("mobile", sPhoneNo);
                                    args.putString("email",sEmailId);
                                    pf.setArguments(args);

                                    //Inflate the fragment
                                    getFragmentManager().beginTransaction().add(R.id.passwordLayout, pf).commit();

                                    // Put Http parameter username with value of Name Edit View control
                                    params.put("name", sName);
                                    // Put Http parameter email with value of Email Edit Value control
                                    params.put("email", sEmailId);
                                    // Put Http parameter mobile with value of Mobile Edit Value control
                                    params.put("mobile", sPhoneNo);
                                    // Invoke Signup RESTful Web Service with Http parameters
                                    signUpWS(params);

                                    //Toast.makeText(getActivity().getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                                    //((MainActivity) getActivity()).navigatetoPasswordActivity();

                                }
                                else {
                                    EmailId.setError("Enter valid Email");
                                }

                            }
                            else {
                                Phone.setError("Enter valid Mobile");
                            }

                        }
                        else {
                            Name.setError("Enter valid Name");
                        }

                    }
                    else {
                        Toast.makeText(getActivity().getApplicationContext(), "Please fill the form, don't leave any field blank", Toast.LENGTH_SHORT).show();
                    }
                }



            });

片段 Y:

//  Retrive the value from fragment x
String strMobile = getArguments().getString("mobile");
                String strEmail = getArguments().getString("email");

请任何人帮助我..

提前致谢!

【问题讨论】:

标签: java android


【解决方案1】:

您可以通过在片段中创建构造函数将值传递给fargment

例如,

对于片段 x 在构造函数中传递值

PasswordFragment pf = new PasswordFragment (name, email, mobile);

对于fragmnet y,您可以从此构造函数中检索值

String name, email, mobile
public PasswordFragment(String name, String email, String mobile) {
        // TODO Auto-generated constructor stub
        this.name=name;
        this.email=email;
        this.mobile=mobile;
}

【讨论】:

  • 这一行足够片段x
  • 我可以把这条线放在哪里?
  • 代替 PasswordFragment pf = new PasswordFragment (); write PasswordFragment pf = new PasswordFragment(姓名、电子邮件、手机);
【解决方案2】:

我知道 Fragment 应该通过它们所附加的 Activity 进行通信;解决您的问题的一种方法是声明

final Activity activity = this.getActivity();

并使用它通过 Activity 属性(您可以选择私有或公共)传递您需要的值,例如

String value1;
String value2; //Into Activity

然后,一旦你填满它们,你就可以从 FragmentY 中检索它们的值。

编辑:当然,在 Fragment 中声明最终 Activity 对象时,您需要对 YourCustomActivity 进行强制转换。

【讨论】:

    【解决方案3】:

    我建议你调查一下。 http://developer.android.com/training/basics/fragments/communicating.html

    它非常干净和可取。任何片段都不应该直接通信。最好的方法是使用调用活动作为两个片段之间的中介

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-31
      • 2016-06-12
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多