这里我从firebase数据库中获取了一些值,现在我想在另一个类中使用这个值。这个实际的代码是什么?
final Vector<String>v_id = new Vector<>();
final Vector<String>v_name = new Vector<>();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
com.google.firebase.database.Query query = databaseReference.child("user").orderByChild("department").equalTo("swe");
query.addChildEventListener(new com.google.firebase.database.ChildEventListener() {
@Override
public void onChildAdded(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
for(com.google.firebase.database.DataSnapshot snapshot:dataSnapshot.getChildren())
{
String id = dataSnapshot.child("id").getValue(String.class);
String name = dataSnapshot.child("name").getValue(String.class);
v_id.add(id);
v_name.add(name);
}
}
答案 0 :(得分:0)
尝试在类似 firebaseutil 的类中创建一个方法作为例子,并在其他类中调用该方法,如下所示:
public class firebaseutils {
public static void getvector(Vector Id, Vector Name){
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
com.google.firebase.database.Query query = databaseReference.child("user").orderByChild("department").equalTo("swe");
query.addChildEventListener(new com.google.firebase.database.ChildEventListener() {
@Override
public void onChildAdded(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
for(com.google.firebase.database.DataSnapshot snapshot:dataSnapshot.getChildren())
{
String id = dataSnapshot.child("id").getValue(String.class);
String name = dataSnapshot.child("name").getValue(String.class);
Id.add(id);
Name.add(name);
}
}
}
}
在其他课程/活动中只需致电:
firebaseutils.getvector(your_vector_id,your_vector_name);