gpt4 book ai didi

java - 如何从一个 fragment 获取值并将其传递给另一个 fragment

转载 作者:行者123 更新时间:2023-12-01 11:14:33 25 4
gpt4 key购买 nike

我有两个 fragment ,一个是 fragment X ,一个是 fragment y。 fragment X 用于使用 Web 服务的注册过程。我想将(电子邮件、手机)相同的值从 fragment X 传递到 fragment Y。

这是我的代码:

fragment 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();
}
}



});

fragment Y:

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

请任何人帮助我..

提前致谢!

最佳答案

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

例如,

对于 fragment 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;
}

关于java - 如何从一个 fragment 获取值并将其传递给另一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31986005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com