无论我输入什么内容,我都会收到奇怪的值。 错误 - 通过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:在我运行的所有这些测试中,我从未将所有字段留空。
在第一次调用'如果''而不是如果( tableNumberText.getText()。toString()!= null || tableNumberText.getText()。toString()!=“”)' –
@IntelliJAmiya不,它没有解决这个错误。我试过了。 –
有什么奇怪的... ...为你的问题添加更多细节...首先它总是让你把空格放在edittexts ..根据你当前的代码。 –