谁能告诉我怎样把对话框的数据保存成字符串吗。我创建了一个二维码扫描应用程序中,我想存储的数据扫描二维码。我尝试了以下代码,但显示了一个错误时,试图从对话框中获取数据的字符串。此行出错字符串user_data=data.gettext().tostring();在gettext()处。请告诉我如何解决这个错误。
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result!=null && result.getContents()!=null){
new AlertDialog.Builder(all_Entries.this)
.setTitle("Customer Information")
.setMessage(result.getContents())
//Save Button
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
addDataToDatabase();
}
private void addDataToDatabase() {
String user_data = data.getText().toString();
// Here getText() Function is not working Showing an Error :**Cannot resolve method 'getText' in 'Intent'**
UserData userData = new UserData(user_data);
repository = new MyRepository(MyDatabase.getInstance(getApplicationContext()).myDao());
repository.addUser(userData);
Toast.makeText(all_Entries.this, "Data is Saved", Toast.LENGTH_SHORT).show();
}
})
//Save Button End
.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
}
super.onActivityResult(requestCode, resultCode, data);
}
1条答案
按热度按时间8ftvxx2r1#
@Nullable Intent data
数据不是EditText
或者TextView
这是一个Intent
所以我想data.toString()
会有用的。