如何将您创建的Android项目的ID写入Firebase数据库。

huangapple go评论79阅读模式
英文:

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:

如何将您创建的Android项目的ID写入Firebase数据库。

The created order is entered in the database

如何将您创建的Android项目的ID写入Firebase数据库。

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 = &quot;&quot;;
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,&quot;Order&quot;);
}
});
}
private void  saveOrderInformation(String id, String sender, String date, String model, String adress, String problem){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
HashMap&lt;String,Object&gt; hashMap = new HashMap&lt;&gt;();
hashMap.put(&quot;id&quot;,id);
hashMap.put(&quot;sender&quot;,sender);
hashMap.put(&quot;date&quot;,date);
hashMap.put(&quot;model&quot;,model);
hashMap.put(&quot;adress&quot;,adress);
hashMap.put(&quot;problem&quot;,problem);
reference.child(&quot;Orders&quot;).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(&quot;Orders&quot;).push().getKey();
HashMap&lt;String,Object&gt; hashMap = new HashMap&lt;&gt;();
hashMap.put(&quot;id&quot;,key);
hashMap.put(&quot;sender&quot;,sender);
hashMap.put(&quot;date&quot;,date);
hashMap.put(&quot;model&quot;,model);
hashMap.put(&quot;adress&quot;,adress);
hashMap.put(&quot;problem&quot;,problem);
reference.child(&quot;Orders&quot;).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 = &quot;&quot;;
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(&quot;Orders&quot;);
String id = reference.push().getKey();

and set this id to id in the database by

    HashMap&lt;String,Object&gt; hashMap = new HashMap&lt;&gt;();
hashMap.put(&quot;id&quot;,id);
hashMap.put(&quot;sender&quot;,sender);
hashMap.put(&quot;date&quot;,date);
hashMap.put(&quot;model&quot;,model);
hashMap.put(&quot;adress&quot;,adress);
hashMap.put(&quot;problem&quot;,problem);

and then insert the entire dataset into the database by

reference.child(id).setValue(hashMap);

huangapple
  • 本文由 发表于 2020年4月5日 00:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61031044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定