更改参数后,在edittext字段中存储已编辑的值

时间:2016-07-30 07:49:45

标签: android json android-edittext android-volley

public class ActivityEditParent extends AppCompatActivity {

    private static final String TAG="ActivityEditParent";
    CustomEditText etFirstName,etLastName,etEmail,etPhone;
    public static ConnectionDetector detector;
    private static final String URL = "http://hooshi.me.bh-in-13.webhostbox.net/index.php/parents/editprofile";
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private CustomButton btnSave;
    private String parentFirstName,parentLastName,parentPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_parent);
        getSupportActionBar().hide();
        detector = new ConnectionDetector(ActivityEditParent.this);
        getUIComponents();
    }

    private void getUIComponents(){

        etFirstName = (CustomEditText) findViewById(R.id.edit_first_name);
        etLastName = (CustomEditText) findViewById(R.id.edit_last_name);
        etEmail = (CustomEditText) findViewById(R.id.edit_email_address);
        etPhone = (CustomEditText) findViewById(R.id.edit_phone_number);
        btnSave = (CustomButton) findViewById(R.id.btn_save_parent);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editParent();

            }
        });


        TextView title = (TextView) findViewById(R.id.toolbar_title);
        ImageButton back = (ImageButton) findViewById(R.id.toolbar_back);
        title.setText("Edit parent");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });

        sharedPreferences = getSharedPreferences(AppConstants.OOSH_PREFERENCES, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        String fName = sharedPreferences.getString(AppConstants.PARENT_FNAME,AppConstants.fName);
        String lName = sharedPreferences.getString(AppConstants.PARENT_LNAME,AppConstants.lName);
        String email = sharedPreferences.getString(AppConstants.PARENT_EMAIL,AppConstants.email);
        String phone = sharedPreferences.getString(AppConstants.PARENT_MOBILE,AppConstants.mobile);

        etFirstName.setText(fName);
        etLastName.setText(lName);
        etPhone.setText(phone);
        etEmail.setText(email);

    }


    private void goBack() {
       startActivity(new Intent(getApplicationContext(), ActivityEditDetails.class));
       finish();
    }

    private void editParent(){
        SharedPreferences preferences = getSharedPreferences(AppConstants.OOSH_PREFERENCES, Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = preferences.edit();
        JSONObject jsonParam = null;

        parentFirstName = etFirstName.getText().toString().trim();
        parentLastName = etLastName.getText().toString().trim();
        parentPhone = etPhone.getText().toString().trim();


        if (detector.checkInternet()){
            jsonParam = new JSONObject();
            JSONObject header = new JSONObject();

            try {
                jsonParam.put("parentId",preferences.getString(AppConstants.PARENT_ID,""));
                jsonParam.put("parentFN",parentFirstName);
                jsonParam.put("parentLN",parentLastName);
                jsonParam.put("parentPhone",parentPhone);
                jsonParam.put("apiAccessKey",preferences.getString(AppConstants.API_ACCESS_KEY,""));
                header.put("parent",jsonParam);
                Log.d("POST PARAMETERS:",""+header);
            } catch (JSONException e) {
                e.printStackTrace();
            }

           JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL, header, new Response.Listener<JSONObject>() {
               @Override
               public void onResponse(JSONObject response) {
                   Log.d("Response:",""+response);

                   String json_status = null;
                   try {
                       json_status = response.getString("status");
                       if (json_status.equalsIgnoreCase("Success")){
                           Toast.makeText(getApplicationContext(), "changed parent details successfully", Toast.LENGTH_SHORT).show();
                           startActivity(new Intent(getApplicationContext(),ActivityHome.class));
                       }

                   } catch (JSONException e) {
                       e.printStackTrace();
                   }


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

           VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
        }
    }
}

在回复获得成功消息后,我想在各自的编辑文本字段中保存已编辑的详细信息,请帮助。成功消息后,我通过意图移动到主屏幕,然后我又回到此屏幕显示以前的详细信息。

2 个答案:

答案 0 :(得分:0)

您希望在从网络下载后或在编辑文本字段中进行编辑后保存数据吗?

  1. 如果来自网络,请在下载数据成功的代码中添加一些保存方法执行语句

     if (json_status.equalsIgnoreCase("Success")){
                   Toast.makeText(getApplicationContext(), "changed parent details successfully", Toast.LENGTH_SHORT).show();
    
    
                   // here you have successful received data so u can map them to edit text or save in shared prefs  
                   // add method to save data here 
                   saveData(response);
    
                   startActivity(new Intent(getApplicationContext(),ActivityHome.class));
               }
    
    
    private void saveData(JSONObject response) {
    
        // use JSon response object save here to shared preferences 
        // see how to load data from json object 
        // https://processing.org/reference/JSONObject_getString_.html
    
        SharedPreferences sharedPreferences = getSharedPreferences(AppConstants.OOSH_PREFERENCES, Context.MODE_PRIVATE);
    
        sharedPreferences.putString(....).apply;
    
    
       // or set edit text controls with JSon data 
    }
    
  2. 要保存编辑文字更改,您有两个选择:

    • 添加到布局保存按钮(您有一个)按钮并使用方法设置点击监听器以保存数据:

      btnSave = (CustomButton) findViewById(R.id.btn_save_parent);
      btnSave.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              saveData(v);
          }
      });
      
      
      private void saveData(View view) {
          // get root view 
          View rootView = view.getRootView();
          // find edit text widget on root view 
          EditText etFirstName = (EditText) rootView.findViewById(R.id.edit_first_name);
          // get string from edit text widget 
          String firstName =  etFirstName.getString.toString();
      
         // get shared preferences 
         SharedPreferences sharedPreferences = getSharedPreferences(AppConstants.OOSH_PREFERENCES, Context.MODE_PRIVATE);
         // save to shared prefs firstName wher NAME_KEY is string to identified your saved data for later use - to load 
         sharedPreferences.putString(NAME_KEY,firstName).apply;
      
      }
      
      
      private void loadDataFromSharedPredferences(View view) {
          // get shared preferences 
          SharedPreferences sharedPreferences = getSharedPreferences(AppConstants.OOSH_PREFERENCES, Context.MODE_PRIVATE);
          // load data from shared prefs firstName wher NAME_KEY is string to identified data to load 
          String  firstName = sharedPreferences.getString(NAME_KEY,firstName);
          // get root view 
          View rootView = view.getRootView();
          // find edit text widget on root view 
          EditText etFirstName = (EditText) rootView.findViewById(R.id.edit_first_name);
          // set string to edit text widget 
          etFirstName.setText(firstName);         
      }
      
    • 为edittext小部件添加文本更改侦听器

  3. <强> PS。你需要澄清 - 写下你想要实现的目标的确切步骤

    • 从网络加载数据?然后保存?
    • 或保存请求日期?
    • 或保存结果数据?

答案 1 :(得分:0)

这一切都发生在这里:

...
if (json_status.equalsIgnoreCase("Success")){
    Toast.makeText(getApplicationContext(), "changed parent details successfully", Toast.LENGTH_SHORT).show();
    startActivity(new Intent(getApplicationContext(),ActivityHome.class));
}
...

成功回复后,将已编辑的值保存在首选项中,例如etFirstName,将其保存为相应首选项的新值:

...
if (json_status.equalsIgnoreCase("Success")){
    Toast.makeText(getApplicationContext(), "changed parent details successfully", Toast.LENGTH_SHORT).show();
    editor.putString(AppConstants.PARENT_FNAME, parentFirstName);
    editor.apply(); //don't forget this
    startActivity(new Intent(getApplicationContext(),ActivityHome.class));
}
...

您以任何方式创建编辑器但不使用它。