Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从模型类到片段获取数据_Android_Android Fragments - Fatal编程技术网

Android 从模型类到片段获取数据

Android 从模型类到片段获取数据,android,android-fragments,Android,Android Fragments,我在从存储用户详细信息的ModelLogin类中获取数据时遇到了一个问题,现在我想在Profile Fragment上显示该详细信息。如何从ModelLogin获取值 我只想要名字,姓氏,EmailId ModelLogin.class public class ModalLogin implements Parcelable { @SerializedName("result") @Expose private String result; @Seriali

我在从存储用户详细信息的
ModelLogin
类中获取数据时遇到了一个问题,现在我想在
Profile Fragment
上显示该详细信息。如何从
ModelLogin
获取值

我只想要名字,姓氏,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);
    }
}
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);
    }

}

您可以在片段初始化时初始化同一个类实例:

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

}

谢谢:)

我想获取电子邮件地址、姓氏、姓氏我该怎么办that@Sourabh斯瓦米:只需调用
mModelLogin.getEmail()
即可发送电子邮件,其他人也一样。感谢它的工作原理@Sourabh-Swami:很高兴!