2016-07-30 3 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. 成功したセーブ追加した場合、次の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 @あなたは1つのエディットテキストフィールドに表示することができ、編集テキストフィールドにテキストを変更した後にデータを保存してくださいしたい - ありがとう....それが動作編集 – 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

あなたはそれを歓迎、大変うれしく思っております助けてください。 –

関連する問題