Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 从模型类获取数据到片段大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我从ModelLogin类中获取数据时出现问题,现在存储用户详细信息我想在Profile Fragment上显示详细信息如何从modelLogin获取值.

我只想要FirstName,LastName,EmailId.

@H_386_5@modelLogin.class

@H_675_11@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); } }

在这是我的片段..

ProfileFragment.class

@H_675_11@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初始化时初始化相同的类实例

@H_675_11@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,String param2,ModelLogin mModelLogin) { MyProfileFragment fragment = new MyProfileFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1,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,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 – 从模型类获取数据到片段全部内容,希望文章能够帮你解决android – 从模型类获取数据到片段所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。