0

無論我輸入什麼內容,我都會收到奇怪的值。 Backend screenshot錯誤 - 通過JSON對象將SharedPreference數據推送到後端

注: 「零」 實際上是一個字符串我已經設置爲默認

代碼,我保存在SharedPreference

 billText = (EditText) findViewById(R.id.billNumber); 
     tableNumberText = (EditText) findViewById(R.id.tableNumber); 
     amountText = (EditText) findViewById(R.id.amount); 
     remarksText = (EditText) findViewById(R.id.remarks); 
     submit = (Button) findViewById(R.id.submitButton); 

     SharedPreferences sharedPreferences = getSharedPreferences(SharedPreference.Form.FORM, MODE_PRIVATE); 
     final SharedPreferences.Editor editor = sharedPreferences.edit(); 

     submit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (tableNumberText.getText().toString() != null || tableNumberText.getText().toString() != "") { 
        editor.putString(SharedPreference.Form.TABLE_NUMBER, tableNumberText.getText().toString()); 
        Toast.makeText(FormActivity.this, "ABC", Toast.LENGTH_LONG).show(); 
       } 

       if (amountText.getText().toString() != null || amountText.getText().toString() != "") { 
        editor.putString(SharedPreference.Form.AMOUNT, amountText.getText().toString()); 
       } 

       if (billText.getText().toString() != null || billText.getText().toString() != "") { 
        editor.putString(SharedPreference.Form.BILL_NUMBER, billText.getText().toString()); 
       } 

       if (remarksText.getText().toString() != null || remarksText.getText().toString() != "") { 
        editor.putString(SharedPreference.Form.REMARKS, remarksText.getText().toString()); 
       } 

       editor.commit(); 
       startActivity(new Intent(getApplicationContext(), FeedbackActivity.class)); 
      } 
     }); 

代碼,我推數據

    jsonObj.put("bill", sharedPreferences.getString(SharedPreference.Form.BILL_NUMBER, "null")); 
        jsonObj.put("table", sharedPreferences.getString(SharedPreference.Form.TABLE_NUMBER, "null")); 
        jsonObj.put("amount", sharedPreferences.getString(SharedPreference.Form.AMOUNT, "null")); 
        jsonObj.put("remarks", sharedPreferences.getString(SharedPreference.Form.REMARKS, "null")); 
        editor.clear(); 
        editor.commit(); 

編輯

我打算做什麼:通過SharedPreference持久保存數據。我首先通過Activity(第一塊代碼)保存數據,然後將數據推送到後端。有四個文本字段(EditText)從我得到String數據。

預期的行爲:當我使用方法

jsonObj.put("key", SharedPreference.getString(key, default-value)

我應該得到我已經保存在後臺把活動的價值。

結果

在後端,而不是讓我進去,我得到了在屏幕截圖所示。

奇怪的一部分:雖然,你可以看到,代碼是相同的所有四個字段具有相同的代碼(有些情況下用戶輸入數據的四個EditText場)他們三人得到默認的字符串,即"null"和一個(金額)保持空白。

PS:在我運行的所有這些測試中,我從未將所有字段留空。

+0

在第一次調用'如果''而不是如果( tableNumberText.getText()。toString()!= null || tableNumberText.getText()。toString()!=「」)' –

+0

@IntelliJAmiya不,它沒有解決這個錯誤。我試過了。 –

+0

有什麼奇怪的... ...爲你的問題添加更多細節...首先它總是讓你把空格放在edittexts ..根據你當前的代碼。 –

回答

0

測試如果一個EditText目前是空的正確方法是:(TextUtils.isEmpty(tableNumberText.getText()的toString()))

if(TextUtils.isEmpty(amountText.getText())) {...} 
+0

這並沒有解決這個錯誤。問題依然存在。 –

+0

你沒有描述實際的問題,除了說結果是「奇怪的」。請提供有關預期和當前行爲的更多信息。 – BladeCoder

+0

請閱讀增加的細節(從**編輯**)。謝謝您的幫助。 –