英文:
How to write the id of the Android item you are creating to the Firebase database
问题
以下是您提供的内容的翻译:
我使用实时数据库。 Java。
问题是我无法获取已创建的ID。我有一个创建订单的用户:
已创建的订单已输入数据库:
它包括信息,发送者的ID,还必须在屏幕截图中显示它自己的ID,这就是我遇到的问题。
以下是订单创建代码:
private EditText mDateField, mModelField, mProblemField, mAdressField;
private Button mConfirm;
private FirebaseUser fuser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
mDateField = findViewById(R.id.date);
mModelField = findViewById(R.id.model);
mProblemField= findViewById(R.id.problem_desc);
mAdressField = findViewById(R.id.adress);
mConfirm = findViewById(R.id.confirm);
fuser = FirebaseAuth.getInstance().getCurrentUser();
final String userID = fuser.getUid();
final String orderid = "";
mConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String date = mDateField.getText().toString();
String model = mModelField.getText().toString();
String address = mAdressField.getText().toString();
String problem = mProblemField.getText().toString();
saveOrderInformation(orderid, userID, date, model, address, problem);
finish();
AppUtilities.showToast(OrderActivity.this, "Order");
}
});
}
private void saveOrderInformation(String id, String sender, String date, String model, String address, String problem) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("id", id);
hashMap.put("sender", sender);
hashMap.put("date", date);
hashMap.put("model", model);
hashMap.put("address", address);
hashMap.put("problem", problem);
reference.child("Orders").push().setValue(hashMap);
}
英文:
I use the Realtime DataBase. Java.
The problem is that I can't get the created ID. I have a user who creates an order:
The created order is entered in the database
It includes information, the sender's ID, and must also have its own ID shown in the screenshot, which is what I have a problem with.
Order creation code below
private EditText mDateField, mModelField, mProblemField, mAdressField;
private Button mConfirm;
private FirebaseUser fuser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
mDateField = findViewById(R.id.date);
mModelField = findViewById(R.id.model);
mProblemField= findViewById(R.id.problem_desc);
mAdressField = findViewById(R.id.adress);
mConfirm = findViewById(R.id.confirm);
fuser = FirebaseAuth.getInstance().getCurrentUser();
final String userID = fuser.getUid();
final String orderid = "";
mConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String date = mDateField.getText().toString();
String model = mModelField.getText().toString();
String adress = mAdressField.getText().toString();
String problem = mProblemField.getText().toString();
saveOrderInformation(orderid,userID,date,model,adress,problem);
finish();
AppUtilities.showToast(OrderActivity.this,"Order");
}
});
}
private void saveOrderInformation(String id, String sender, String date, String model, String adress, String problem){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
HashMap<String,Object> hashMap = new HashMap<>();
hashMap.put("id",id);
hashMap.put("sender",sender);
hashMap.put("date",date);
hashMap.put("model",model);
hashMap.put("adress",adress);
hashMap.put("problem",problem);
reference.child("Orders").push().setValue(hashMap);
}
}
答案1
得分: 0
如果您希望push()
也是属性id
的值,您可以尝试以下操作:
private void saveOrderInformation(String id, String sender, String date, String model, String address, String problem) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
String key = reference.child("Orders").push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("id", key);
hashMap.put("sender", sender);
hashMap.put("date", date);
hashMap.put("model", model);
hashMap.put("address", address);
hashMap.put("problem", problem);
reference.child("Orders").child(key).setValue(hashMap);
}
您可以使用getKey()
获取push()
的值,然后将其赋值给id
,并在设置值时将其用作引用。
英文:
If you want the push()
to be also the value of the attribute id
, then you can try the following:
private void saveOrderInformation(String id, String sender, String date, String model, String adress, String problem){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
String key = reference.child("Orders").push().getKey();
HashMap<String,Object> hashMap = new HashMap<>();
hashMap.put("id",key);
hashMap.put("sender",sender);
hashMap.put("date",date);
hashMap.put("model",model);
hashMap.put("adress",adress);
hashMap.put("problem",problem);
reference.child("Orders").child(key).setValue(hashMap);
}
You can get the push()
value using getKey()
and then you can assign it to id
and use it as reference when setting the value.
答案2
得分: 0
id因为你在 final String orderid = "";
中将其设置为空,所以现在是空白的。
你可以删除那行代码,或者你可以在数据库中删除 sender 列并将其设置为 id。
或者如果你想要将 orderID 设置为数据库中的 id。然后使用以下函数获取 id:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Orders");
String id = reference.push().getKey();
然后通过以下方式将此 id 设置为数据库中的 id:
HashMap<String,Object> hashMap = new HashMap<>();
hashMap.put("id",id);
hashMap.put("sender",sender);
hashMap.put("date",date);
hashMap.put("model",model);
hashMap.put("adress",adress);
hashMap.put("problem",problem);
然后通过以下方式将整个数据集插入数据库:
reference.child(id).setValue(hashMap);
英文:
id is blank because you've set it to blank on final String orderid = "";
Either, you can delete that line or you can remove the sender column in the database and set it to id.
Or if you want to set the orderID as id in the database. Then use the below function to obtain the id
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Orders");
String id = reference.push().getKey();
and set this id to id in the database by
HashMap<String,Object> hashMap = new HashMap<>();
hashMap.put("id",id);
hashMap.put("sender",sender);
hashMap.put("date",date);
hashMap.put("model",model);
hashMap.put("adress",adress);
hashMap.put("problem",problem);
and then insert the entire dataset into the database by
reference.child(id).setValue(hashMap);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论