简体   繁体   English

我想获得 push() 制作的唯一 ID

[英]I want to get a unique id that push() had made

I'm leaning Firebase.我倾向于 Firebase。

I pushed some data in db like below我在数据库中推送了一些数据,如下所示

 -test -LBpjFMIPq-kQWyBmqf7 -str : value-a

but dataSnapshot.getKey() always get repository's name the "test".但 dataSnapshot.getKey() 总是将存储库的名称命名为“测试”。

How can I get the unique id that push() had made?如何获得 push() 制作的唯一 ID?

unique id is LBpjFMIPq-kQWyBmqf7 in this case.在这种情况下,唯一 id 是 LBpjFMIPq-kQWyBmqf7。

package com.example.happy.firebasetest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;


public class MainActivity extends AppCompatActivity {
    String key;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference ref1 = database.getReference("test");

        ref1.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                key = dataSnapshot.getKey();
                System.out.println(key);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

so key所以关键

Try the following:请尝试以下操作:

ref1.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
             for(DataSnapshot datas: dataSnapshot.getChildren()){
            key = datas.getKey();
            System.out.println(key);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

You need to loop inside the push() key then use the method getKey() to retrieve the source location which is the unique id.您需要在push()键内循环,然后使用方法getKey()检索作为唯一 ID 的源位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想创建唯一的用户ID - I want to create unique user id 我想获得用户唯一的ID。 我如何使用getKey方法获取它 - i want to get the user unique id. how do i fetch it using getKey method SQLite,不想获取最大_id,我想获取“下一个” _id - SQLite, Don't want to get max _id, I want to get the “next” _id 我想得到一个字符串的字符。 例如,如果我有str = aabbc,那么答案是abc - i want to get the character that a string is made of. for example if i have str=aabbc then the answer is abc 当我单击RecyclerView项时,我想获取ID - I want to get the id when I click a RecyclerView item 如何获得每个类扩展的唯一 ID? - How do I get an unique ID per class extention? 如何在Alfresco取回任何文件的唯一ID? - How can i get back the unique Id of any file at Alfresco? 我希望能够撤消在JTextArea中所做的更改 - I want to be able to undo changes made in JTextArea 如果我知道数据存储区实体的唯一ID,该如何获取它? - How do I get a Datastore Entity by its unique ID if I know the ID? 获取Access数据库中的下一个ID(现在的ID为M15,我想获取M16) - Get the next ID in Access database(now the ID is M15, i want get M16)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM