我正在从 FirebaseDatabase
中获取一些数据,然后将它们放入一个 array
中,然后尝试将它们显示在一个 List
中,它是在自定义 AlertDialog
中。
代码如下:
query = mDatabase.child("child").child(anotherChild).child("yetAnotherChild");
uProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
query.orderByChild("someChild").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null) {
Map<String, String> newD = (Map<String, String>) dataSnapshot.getValue();
ArrayList<String> l = new ArrayList<String>();
l.add(newD.get("lol").substring(30));
String names[] = l.toArray(new String[0]);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Activity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = inflater.inflate(R.layout.dialog_list, null);
alertDialog.setView(convertView);
alertDialog.setTitle("title");
ListView lv = (ListView) convertView.findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, names);
lv.setAdapter(adapter);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
alertDialog.show();
} else {
Toast.makeText(getBaseContext(), "NULLLLL", Toast.LENGTH_SHORT).show();
}
}
...
...
});
}
});
这是数据库结构:
app
-child
-anotherChild
-yetAnotherChild
-inaccessibleChild
-someChild: "value"
-lol: "value"
我无法在此处使用 valueEventListener()
,因为我无权访问 inaccessibleChild
。此处的inaccessibleChild
是关注特定用户的其他用户的uid
。我怎样才能访问那里的 uid
?
问题 数据正在获取,但不是在一个 AlertDialog
中显示在列表中,而是在 中一个接一个地显示3 单独的AlertDialog
。
这里出了什么问题?
请告诉我。
最佳答案
Firebase 事务是异步的,因此您添加 3 个子项的初始行发生在您设置监听器之后,因此您的回调被调用了 3 次。 (制作 3 个对话)。
将此行移到点击之外:
query = mDatabase.child("child").child(anotherChild).child("yetAnotherChild");
然后当你添加下面的监听器时(在点击里面)它应该没问题
query.orderByChild("someChild").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
关于android - 从 Firebase 数据库获取的数据显示在 3 个单独的警报对话框中,而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41401928/