使用嵌套addValueEventListener

时间:2018-01-30 09:40:36

标签: java android firebase firebase-realtime-database

我正在使用FireBase使用Android应用程序。在我添加的数据库中,我向其中一个用户出版物添加了两个表。我将发布信息和URI的数量放在一起,以便通过另一个表格为每个用户的信息检索信息。当我这样做时,我需要使用数据库事件

(.addValueEventListener).

嵌套但是我收到错误

  

(错误:(157,33)错误:局部变量UserName是从内部类中访问的;需要声明为final)

这是使用的代码

  private void UserPostInformation(final String key) {
    DatabaseReference historyDatabase = FirebaseDatabase.getInstance().getReference().child("ThePostting").child(key);
    historyDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            if (dataSnapshot.exists())
            {
                String IdHistory = dataSnapshot.getKey();
                Long time = 0L;
                String foodname11 = "";
                String imagefood1 = "";
                String s = null;
                String name = "name";
                String UserName = "";
                for (DataSnapshot  child : dataSnapshot.getChildren())
                {
                    if (child.getKey().equals("time")) {
                        time = Long.valueOf(child.getValue().toString());

                    }
                    if (child.getKey().equals("FoodName")) {
                        foodname11 = child.getValue().toString();

                    }

                    if (child.getKey().equals("profileImageUrl")) {
                        imagefood1 = child.getValue().toString();
                    }
                    if (child.getKey().equals("UserID")) {


                        s = child.getValue().toString();

                    }
                }

                UserIDGetInfo =  s;


                DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(UserIDGetInfo);
                databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        if (dataSnapshot.exists())
                        {

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

                            if (map.get("name")!=null)
                            {

      //error is her where using the String UserName get the error  local variable UserName is accessed from within inner class; needs to be declared final)
                            UserName= map.get("name").toString();
                        }
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

                HistoryObject obj =new HistoryObject(IdHistory , getDate(time),foodname11 , imagefood1 , UserName);
                resultHistory.add(obj);
                mhistoryAdapter.notifyDataSetChanged();

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

数据库的这一部分我使用 enter image description here

2 个答案:

答案 0 :(得分:1)

UserName声明为该类的属性,您也可以正确获取UserName的空值,因为您在加载User之前设置了HistoryObject的实例

结帐我的修复

private String UserName = "";
private void UserPostInformation(final String key) 
{
    DatabaseReference historyDatabase = FirebaseDatabase.getInstance().getReference().child("ThePostting").child(key);
    historyDatabase.addValueEventListener(new ValueEventListener() 
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            if (dataSnapshot.exists())
            {
                String IdHistory = dataSnapshot.getKey();
                Long time = 0L;
                String foodname11 = "";
                String imagefood1 = "";
                String s = null;
                String name = "name";
                UserName = "";
                for (DataSnapshot  child : dataSnapshot.getChildren())
                {
                    if (child.getKey().equals("time")) {
                        time = Long.valueOf(child.getValue().toString());

                    }
                    if (child.getKey().equals("FoodName")) {
                        foodname11 = child.getValue().toString();

                    }

                    if (child.getKey().equals("profileImageUrl")) {
                        imagefood1 = child.getValue().toString();
                    }
                    if (child.getKey().equals("UserID")) {


                        s = child.getValue().toString();

                    }
                }

                UserIDGetInfo =  s;


                DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(UserIDGetInfo);
                databaseReference.addListenerForSingleValueEvent(new ValueEventListener() 
                {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        if (dataSnapshot.exists())
                        {
                            Map<String,Object> map = (Map<String,Object>) dataSnapshot.getValue();
                            if (map.get("name")!=null)
                            {
                                UserName= map.get("name").toString();
                            }


                            HistoryObject obj = new HistoryObject(IdHistory , getDate(time),foodname11 , imagefood1 , UserName);
                            resultHistory.add(obj);
                            mhistoryAdapter.notifyDataSetChanged();

                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });


            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

答案 1 :(得分:0)

要么将username声明为类变量,要么将修饰符更改为final,错误是谨慎的local variable UserName is accessed from within inner class; needs to be declared final 因此将其声明为final Stirng userName