从FirebaseDatabase获取的数据将显示在3个单独的AlertDialog中,而不是一个 [英] Data fetched from FirebaseDatabase is getting shown in 3 separate AlertDialogs instead of one

查看:296
本文介绍了从FirebaseDatabase获取的数据将显示在3个单独的AlertDialog中,而不是一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 FirebaseDatabase 获取一些数据,然后将它们放入数组中,然后尝试显示它们在一个自定义 AlertDialog 中的 List



以下是代码:

  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){$ b $ if(dataSnapshot!= null){

Map< String,String> newD =(Map< String,String>)dataSnapshot.getValue();

ArrayList< String> l = new ArrayList< String>($;
l.add ();

字符串名称alertDialog = new AlertDialog.Builder(Activity.this);
LayoutInflater inflater = getLayoutInflater();
查看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();
}
}

...
...
});

}
});

以下是数据库结构:

<$ p $ app
-child
-anotherChild
-yetAnotherChild
-inaccessibleChild
-someChild:value
-lol: value

我无法使用 valueEventListener()在这里,因为我没有访问 inaccessibleChild inaccessibleChild 这里是其他用户的 uid 。我怎样才能访问 uid



问题在于数据正在被抓取但不是在 AlertDialog 列表中显示,而是在 3 单独的 AlertDialog



这里出了什么问题?

请让我知道。


<上面的答案都没有帮助,尽管Linxy的回答是正确的,但我在解决问题后看到了这个答案。

把所有的代码移出来,只写这行: alertDialog.show(); 里面 setOnClickListener()改进我的代码:

  query = mDatabase.child(child ).child(anotherChild).child( yetAnotherChild); 

query.orderByChild(someChild)。addChildEventListener(new ChildEventListener(){
@Override $ b $ 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();
查看convertView = inflater.inflate (R.layout.dialog_list,null);
alertDialog.setView(convertView);
aler tDialog.setTitle( 标题);
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){

}
});
} else {
Toast.makeText(getBaseContext(),NULLLLL,Toast.LENGTH_SHORT).show();
}
}

...
...
});

uProfile.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
alert11.show();
}
});

无论如何,我要感谢所有回答这个问题的人。



和平。

I'm fetching some data from FirebaseDatabase and then putting them into an array and then trying to show them in a List which is in a custom AlertDialog.

Here's the code:

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();
                }
            }

            ...
            ...
        });

    }
});

Here's the database structure:

app
 -child
   -anotherChild
     -yetAnotherChild
       -inaccessibleChild
         -someChild: "value"
         -lol: "value"

I can't use valueEventListener() here as I have no access to inaccessibleChild. The inaccessibleChild here is the uid of the other users who followed a particular user. How can I access there uid?

The problem is that data is getting fetched but instead of getting shown in a list in one AlertDialog, it is getting shown one-by-one in 3 separate AlertDialog.

What is going wrong here?

Please let me know.

解决方案

Well, none of the above answers really helped, though Linxy's answer is correct, I saw it after solving the probelm.

Moving all the code out and writing just this line: alertDialog.show(); inside setOnClickListener() improving my code to this:

    query = mDatabase.child("child").child(anotherChild).child("yetAnotherChild");

    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) {

                            }
                        });
                    } else {
                        Toast.makeText(getBaseContext(), "NULLLLL", Toast.LENGTH_SHORT).show();
                    }
                }

                ...
                ...
            });

uProfile.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        alert11.show();
    }
});

Anyway, I would like to thank all those who answered this question.

Peace.

这篇关于从FirebaseDatabase获取的数据将显示在3个单独的AlertDialog中,而不是一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆