使用另一个片段的会话更新一个片段部分

时间:2019-02-05 13:13:33

标签: java android-studio

我正在使用Volley收集数据。我有片段的编辑配置文件部分,其侧面是主配置文件。我可以成功更新个人资料,但是当我在主个人资料中滑动时,旧数据仍然存在。我应该投入些什么来更新片段的mainprofile端?

这是我有MainProfileFragment的MainProfile片段

这是它的代码:

public class MainProfileFragment extends  Fragment{

private static final String TAG = "MainProfileFragment";
SessionManager sessionManager;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_mainprofile, container, false);
    return view;

}

@NonNull
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {


    sessionManager = new SessionManager(getActivity());

    TextView name = (TextView) getView().findViewById(R.id.name);
    TextView phoneNumber = (TextView) getView().findViewById(R.id.number);
    TextView gender = (TextView) getView().findViewById(R.id.gender);
    TextView address = (TextView) getView().findViewById(R.id.address);
    TextView occupation = (TextView) getView().findViewById(R.id.occupation);
    TextView birthDate = (TextView) getView().findViewById(R.id.birthDate);
    TextView userType = (TextView) getView().findViewById(R.id.userType);
    TextView id = (TextView) getView().findViewById(R.id.userID);
    Button logout = (Button) getView().findViewById(R.id.logoutBtn);

    HashMap<String, String> user = sessionManager.getUserDetail();
    String mName = user.get(sessionManager.NAME);
    String mNumber = user.get(sessionManager.NUMBER);
    String mGender = user.get(sessionManager.GENDER);
    String mAddress = user.get(sessionManager.ADDRESS);
    String mOccupation = user.get(sessionManager.OCCUPATION);
    String mBirthDate = user.get(sessionManager.BIRTHDATE);
    String mUserType = user.get(sessionManager.USERTYPE);
    String mUserID = user.get(sessionManager.ID);


    id.setText(mUserID);
    name.setText(mName);
    phoneNumber.setText(mNumber);
    gender.setText(mGender);
    address.setText(mAddress);
    occupation.setText(mOccupation);
    birthDate.setText(mBirthDate);
    userType.setText(mUserType);





    logout.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), LogoutEffect.class);
            sessionManager.logout();
            startActivity(intent);

        }
    });

}
}

现在旁边是SideProfileFragment,它可以编辑配置文件数据。这是它的代码:

public class SideProfileFragment extends  Fragment{

private static final String TAG = SideProfileFragment.class.getSimpleName();
private EditText name, birthDate, address, occupation, gender, number;
private Button btnSave;


SessionManager sessionManager;
String getId = "";
private static final String URL_READ = "http://isalonbyageeks.000webhostapp.com/readDetail.php";
private static final String URL_EDIT = "http://isalonbyageeks.000webhostapp.com/editDetail.php";

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_sideprofile, container, false);


    return view;
}

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    sessionManager = new SessionManager(getActivity());
    name = (EditText) getView().findViewById(R.id.userName);
    birthDate = (EditText) getView().findViewById(R.id.userBirthDate);
    address = (EditText) getView().findViewById(R.id.userAddress);
    occupation = (EditText) getView().findViewById(R.id.userOccupation);
    gender = (EditText) getView().findViewById(R.id.userGender);
    number = (EditText) getView().findViewById(R.id.userNumber);

    btnSave = (Button) getView().findViewById(R.id.buttonSaveEdit);

    HashMap<String, String> user = sessionManager.getUserDetail();
    getId = user.get(sessionManager.ID);

    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SaveEditProfile();
        }
    });

}
private void getUserDetail(){
    final ProgressDialog progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Loading...");
    progressDialog.show();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_READ,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    Log.i(TAG, response);

                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");
                        JSONArray jsonArray = jsonObject.getJSONArray("read");

                        if(success.equals("1")){
                            for(int i = 0; i < jsonArray.length();i++){

                                JSONObject object = jsonArray.getJSONObject(i);
                                String strName = object.getString ("name");
                                String strNumber = object.getString("phone_number");
                                String strGender = object.getString("gender");
                                String strAddress = object.getString("address");
                                String strOccupation = object.getString("occupation");
                                String strBirthDate = object.getString("birth_date");

                                name.setText(strName);
                                birthDate.setText(strBirthDate);
                                address.setText(strAddress);
                                number.setText(strNumber);
                                gender.setText(strGender);
                                occupation.setText(strOccupation);

                            }
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                        progressDialog.dismiss();
                        Toast.makeText(getActivity(),"Error Reading Detail" +e.toString(), Toast.LENGTH_LONG).show();
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();

                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError{
            Map<String, String> params = new HashMap<>();
            params.put("id", getId);
            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    requestQueue.add(stringRequest);
}

@Override
public void onResume(){
    super.onResume();
    getUserDetail();
}


private void SaveEditProfile(){
    final String name = this.name.getText().toString().trim();
    final String birthdate = this.birthDate.getText().toString().trim();
    final String address = this.address.getText().toString().trim();
    final String number = this.number.getText().toString().trim();
    final String gender = this.gender.getText().toString().trim();
    final String occupation = this.occupation.getText().toString().trim();
    final String id = getId;
    final String userType = sessionManager.USERTYPE;

    final ProgressDialog progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Saving Details...");
    progressDialog.show();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_EDIT,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");
                        if(success.equals("1")){
                            Toast.makeText(getActivity(), "Edit Saved!", Toast.LENGTH_SHORT).show();
                            sessionManager.createSession(id, name, number, gender, address, occupation, birthdate,userType);


                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        progressDialog.dismiss();
                        Toast.makeText(getActivity(), "Error"+e.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    Toast.makeText(getActivity(), "Error "+ error.toString(), Toast.LENGTH_SHORT).show();
                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("name", name);
            params.put("birth_date", birthdate);
            params.put("address", address);
            params.put("number", number);
            params.put("gender", gender);
            params.put("occupation", occupation);
            params.put("id", id);

            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    requestQueue.add(stringRequest);

}

}

我知道我有太多的代码,但是我所需要的只是当我单击保存编辑时,我只希望更新主片段。或者我只是希望我可以像其他应用一样添加一个,就像我向下拖动片段刷新一样。

1 个答案:

答案 0 :(得分:0)

这只是通过使我的PHP代码正确解决! :)

看到我得到的表名和列名:),请小心您穿的家伙!嗨