获取Firebase实时数据库的密钥参考表

时间:2018-04-24 13:34:58

标签: java android firebase firebase-realtime-database

我正在尝试编写一个应用程序,它会从我的Firebase Database中抛出随机用户名。为了做到这一点,我需要从每个用户获取参考密钥,以便我可以将其映射到应用程序的不同部分中的用户名。

对,结果如下:

enter image description here enter image description here

这就是我想要的(当然匹配Toast):

enter image description here

MainActivity

public class MainActivity extends AppCompatActivity {
    private FirebaseAuth mAuth;
    private FirebaseUser mCurrentUser;
    private DatabaseReference mUsersDatabase;
    private DatabaseReference mUsersDatabase1;
    private Toolbar mToolbar;
    private String item;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAuth = FirebaseAuth.getInstance();
        mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
        mToolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(this.mToolbar);
        getSupportActionBar().setTitle("TestApp");

        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
        String current_uid = mCurrentUser.getUid(); 
        mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);

        mUsersDatabase1 = FirebaseDatabase.getInstance().getReference().child("Users");

    }

丢弃随机用户名的方法

public void getRandomUsernames(){

        mUsersDatabase1.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                ArrayList<String> userlist = new ArrayList<String>();
                for(DataSnapshot dsp : dataSnapshot.getChildren()){
                    userlist.add(String.valueOf(dsp.getValue()));
                }

                Object randomitem = userlist.get(new Random().nextInt(userlist.size()));
                Toast.makeText(getBaseContext(), randomitem.toString(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

}

我尝试的一切都没有成功。任何建议表示赞赏

2 个答案:

答案 0 :(得分:0)

您是否尝试过getKey()?

public void onDataChange(DataSnapshot dataSnapshot) {
    ArrayList<String> userlist = new ArrayList<String>();
    ArrayList<String> keylist = new ArrayList<String>();
    for(DataSnapshot dsp : dataSnapshot.getChildren()) {
        userlist.add(String.valueOf(dsp.getValue()));
        keylist.add(dsp.getKey());
    }
    int randpos = new Random().nextInt(userlist.size());
    Object randomitem = userlist.get(randpos);
    String key = keylist.get(randpos);
    Toast.makeText(getBaseContext(), randomitem.toString() + " key:" + key, Toast.LENGTH_LONG).show();
}

答案 1 :(得分:0)

获取密钥只需执行此操作:

   DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");

reference.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
   String keys=datas.getKey();
  }
 }
@Override
public void onCancelled(DatabaseError databaseError) {
  }
});

for循环,将使您能够在随机密钥内迭代。此String keys=datas.getKey();将返回密钥。

  

public String getKey ()

     

此快照源位置的密钥名称

更多信息:

https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot.html#getKey()