2016-07-30 68 views
0
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); 
     } 
    } 
} 

在响应中获取成功消息后,我希望将编辑的详细信息保存在各自的编辑文本字段中,请帮助。 成功消息后,我通过意向移动到主屏幕,然后回到此屏幕,它仅显示以前的详细信息。更改参数后在edittext字段中存储编辑的值

回答

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小部件

PS。你需要澄清 - 写一个你想要做什么的确切步骤

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

我想要在编辑文本字段中更改文本后保存数据,您可以显示一个编辑文本字段请 – ajay110125

+0

@ ajay110125 - 请参阅编辑 – ceph3us

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

任何你创建编辑器的方式,但不使用它。

+0

谢谢....它的工作原理 – ajay110125

+0

欢迎您,很高兴它帮助。 –

相关问题