Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Data from Model Class to Fragment

I've getting a problem to fetch data from my ModelLogin class in which user details are stored now i want to show that detail on Profile Fragment how can i get values from modelLogin.

And I am only want 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);
    }
}

and now here is my 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);
    }

}
like image 764
Sourabh Avatar asked Jul 05 '16 04:07

Sourabh


1 Answers

You can just initialize same class instance while Fragment initialization:

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

}

Thank You :)

like image 90
AndiGeeky Avatar answered Oct 14 '22 08:10

AndiGeeky