gpt4 book ai didi

android - 从模型类获取数据到 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:42 24 4
gpt4 key购买 nike

我在从存储用户详细信息的 ModelLogin 类中获取数据时遇到问题,现在我想在 Profile Fragment 上显示该详细信息我怎样才能得到来自 modelLogin 的值。

我只需要 FirstName, LastName, EmailId。

ModelLogin.class

public class ModalLogin implements Parcelable {


@SerializedName("result")
@Expose
private String result;
@SerializedName("customer-id")
@Expose
private String customerId;
@SerializedName("error_message")
@Expose
private String errorMessage;
@SerializedName("apiName")
@Expose
private String apiName;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("last_name")
@Expose
private String lastName;
@SerializedName("email")
@Expose
private String email;
@SerializedName("quoteId")
@Expose
private Integer quoteId;
@SerializedName("token")
@Expose
private String token;

protected ModalLogin(Parcel in) {
result = in.readString();
customerId = in.readString();
errorMessage = in.readString();
apiName = in.readString();
firstName = in.readString();
lastName = in.readString();
email = in.readString();
token = in.readString();
quoteId = in.readInt();
}

public static final Creator<ModalLogin> CREATOR = new Creator<ModalLogin>() {
@Override
public ModalLogin createFromParcel(Parcel in) {
return new ModalLogin(in);
}

@Override
public ModalLogin[] newArray(int size) {
return new ModalLogin[size];
}
};

/**
*
* @return
* The result
*/
public String getResult() {
return result;
}

/**
*
* @param result
* The result
*/
public void setResult(String result) {
this.result = result;
}

/**
*
* @return
* The customerId
*/
public String getCustomerId() {
return customerId;
}

/**
*
* @param customerId
* The customer-id
*/
public void setCustomerId(String customerId) {
this.customerId = customerId;
}

/**
*
* @return
* The errorMessage
*/
public String getErrorMessage() {
return errorMessage;
}

/**
*
* @param errorMessage
* The error_message
*/
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

/**
*
* @return
* The apiName
*/
public String getApiName() {
return apiName;
}

/**
*
* @param apiName
* The apiName
*/
public void setApiName(String apiName) {
this.apiName = apiName;
}

/**
*
* @return
* The firstName
*/
public String getFirstName() {
return firstName;
}

/**
*
* @param firstName
* The first_name
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}

/**
*
* @return
* The lastName
*/
public String getLastName() {
return lastName;
}

/**
*
* @param lastName
* The last_name
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}

/**
*
* @return
* The email
*/
public String getEmail() {
return email;
}

/**
*
* @param email
* The email
*/
public void setEmail(String email) {
this.email = email;
}

/**
*
* @return
* The quoteId
*/
public String getQuoteId() {
String id = ""+quoteId;
return id;
}

/**
*
* @param quoteId
* The quoteId
*/
public void setQuoteId(String quoteId) {
int id = Integer.parseInt(quoteId);
this.quoteId = id;
}

/**
*
* @return
* The token
*/
public String getToken() {
return token;
}

/**
*
* @param token
* The token
*/
public void setToken(String token) {
this.token = token;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(result);
dest.writeString(customerId);
dest.writeString(errorMessage);
dest.writeString(apiName);
dest.writeString(firstName);
dest.writeString(lastName);
dest.writeString(email);
dest.writeString(token);
dest.writeInt(quoteId);
}
}

现在这是我的 fragment ..

ProfileFragment.class

public class MyProfileFragment extends Fragment {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

private String mParam1;
private String customerId;

private View mLoginFormView ,view;
private ProgressBar mProgressView;
private Button saveAddress,changepassword;
private EditText fName,lName,emailId,mobileNo, oldPWD,newPWD;
private OnFragmentInteractionListener mListener;
private JSONObject jsonObject;
private ModalLogin modelLogin;

public MyProfileFragment() {
// Required empty public constructor
}


// TODO: Rename and change types and number of parameters
public static MyProfileFragment newInstance(String param1, String param2) {
MyProfileFragment fragment = new MyProfileFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
customerId = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my_profile, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initViews(view);

}


private void initViews(View v) {
mLoginFormView = (View)v.findViewById(R.id.mainView);
mProgressView =(ProgressBar) v.findViewById(R.id.login_progress);
view = (View) v.findViewById(R.id.mainV);
saveAddress = (Button)v.findViewById(R.id.button_save);
changepassword = (Button)v.findViewById(R.id.button_changePwd);
fName = (EditText)v.findViewById(R.id.fname);
lName =(EditText)v.findViewById(R.id.lname);
emailId =(EditText)v.findViewById(R.id.email);
mobileNo = (EditText)v.findViewById(R.id.telephone);

changepassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog paswordDialog = new Dialog(getActivity(), R.style.FullHeightDialog);
paswordDialog.setContentView(R.layout.dialog);
paswordDialog.setCancelable(true);
//now that the dialog is set up, it's time to show it
paswordDialog.show();
}
});
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

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

}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}

private void showProgress(final boolean show) {
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
}

}

最佳答案

您可以在 fragment 初始化时只初始化相同的类实例:

public class MyProfileFragment extends Fragment {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// Declare instance here
private ModelLogin mModelLogin;

private String mParam1;
private String customerId;

private View mLoginFormView ,view;
private ProgressBar mProgressView;
private Button saveAddress,changepassword;
private EditText fName,lName,emailId,mobileNo, oldPWD,newPWD;
private OnFragmentInteractionListener mListener;
private JSONObject jsonObject;
private ModalLogin modelLogin;

public MyProfileFragment() {
// Required empty public constructor
}


// TODO: Rename and change types and number of parameters
public static MyProfileFragment newInstance(String param1, String param2,ModelLogin mModelLogin) {
MyProfileFragment fragment = new MyProfileFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);

// Initialize instance here
fragment.mModelLogin = mModelLogin;

return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
customerId = getArguments().getString(ARG_PARAM2);

// Now you can use wherever you want
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my_profile, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initViews(view);

}


private void initViews(View v) {
mLoginFormView = (View)v.findViewById(R.id.mainView);
mProgressView =(ProgressBar) v.findViewById(R.id.login_progress);
view = (View) v.findViewById(R.id.mainV);
saveAddress = (Button)v.findViewById(R.id.button_save);
changepassword = (Button)v.findViewById(R.id.button_changePwd);
fName = (EditText)v.findViewById(R.id.fname);
lName =(EditText)v.findViewById(R.id.lname);
emailId =(EditText)v.findViewById(R.id.email);
mobileNo = (EditText)v.findViewById(R.id.telephone);

changepassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog paswordDialog = new Dialog(getActivity(), R.style.FullHeightDialog);
paswordDialog.setContentView(R.layout.dialog);
paswordDialog.setCancelable(true);
//now that the dialog is set up, it's time to show it
paswordDialog.show();
}
});
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

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

}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}

private void showProgress(final boolean show) {
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
}

}

谢谢你:)

关于android - 从模型类获取数据到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38195356/

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