Firebase侦听器跳过 [英] Firebase Listener Skip

查看:59
本文介绍了Firebase侦听器跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已转换为Firebase进行Android开发,因为我想利用Firebase的许多功能(通知,消息传递,存储等),但是,我很难从Firebase接收数据,也无法在线找到详细信息.是的,我已经尝试过该文档,但是没有解释我遇到的事件.

I have converted to Firebase for my Android development as I want to utilise the many features of Firebase (Notifications, Messaging, Storage etc), however, I am having difficulty receiving data from Firebase and cannot find detailed information online. Yes, I have tried the documentation but it does not explain the events I am experiencing.

我想检索将其插入对象数组列表中的所有数据.这是一个静态示例:

I want to retrieve all the data that I have an insert it into an arraylist of objects. Here is a static example:

users[i] = new User("James" + (i*2.5), "Hmack00" + (i*2), "https://firebasPic_2019.03.03.05.26.35.jpg.jpg?alt=media&token=cf", "United Kingdom"); 

数据库:

   {
      "Users" : {
        "4PdlTlv3qjZ3BmDvrJyUut9Fnq43" : {
          "Country" : "xx",
          "Fullname" : "hh",
          "ProfilePicture" : "htm/o/Images%2FSearchAdapter%2F4PdlTlv3qjZ3BmDvrJyUut9Fnq43%2FProfilePicture%2FProfilePic_2019.03.06.10.47.54.jpg.jpg?alt=media&token=b647708e-c6d5-4b45-bef0-3dc40301b73a",
          "Username" : "hmack001"
        },
        "COg4r4io9hezhFpmK3adPucUXA93" : {
          "Country" : "spain",
          "Fullname" : "nat",
          "ProfilePicture" : "hcom/o/Images%2FSearchAdapter%2FCOg4r4io9hezhFpmK3adPucUXA93%2FProfilePicture%2FProfilePic_2019.03.06.19.14.17.jpg.jpg?alt=media&token=8620b321-5cef-42f0-a828-dbb7c37c8e7d",
          "Username" : "nat"
        },
        "Tw1xRxViygNsLqrQiaaMAvAduIu1" : {
          "Country" : "uk",
          "Fullname" : "harvey\n",
          "ProfilePicture" : "t.com/o/Images%2FUsers%2FTw1xRxViygNsLqrQiaaMAvAduIu1%2FProfilePicture%2FProfilePic_2019.03.03.05.26.35.jpg.jpg?alt=media&token=c290e75a-5f92-4271-bcb5-c644fe1b14ef",
          "Username" : "RGB"
        },
        "vOxr1RoDqgWogKK1lp9pfpTHc6w2" : {
          "Country" : "scotland ",
          "Fullname" : "greg greg",
          "ProfilePicture" : "ot.com/o/Images%2FSearchAdapter%2FvOxr1RoDqgWogKK1lp9pfpTHc6w2%2FProfilePicture%2FProfilePic_2019.03.04.12.30.22.jpg.jpg?alt=media&token=27b024cf-0691-4121-8a27-26acf101ebc2",
          "Username" : "greg"
        },
        "xecUOPeyMcQaQrgkU9ouDgK90Ai1" : {
          "Country" : "ggh",
          "Fullname" : "Da apply ",
          "ProfilePicture" : "2FProfilePic_2019.03.03.04.58.50.jpg.jpg?alt=media&token=f35854c2-3ff9-4d18-9f7a-10c13f066c68",
          "Username" : "gg"
        }
      }
    }

这是我的代码,之后我将解释我的错误(我留在僵尸"代码中,以显示我的尝试)

Here is my code and I will explain my errors after (I have left in 'zombie' code, to show the attempts that I have made)

//Firebase Variables
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;

//Firebase Data
    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    userId = mAuth.getCurrentUser().getUid();
    myRef = mFirebaseDatabase.getReference().child("Users").child(userId);

        //Firebase Data
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) { //Function called every time a change is made to the database
            showData(dataSnapshot);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) { //Function called when there is an error in the database/ with the call
            Log.d("Print","Cancelled Firebase: " + databaseError.toString());
        }
    });
}

private void showData(DataSnapshot dataSnapshot) {
    int userCount = 5;
    int i = 0;
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        //User user = new User();
        //User user = new User(); //Get Count of Firebase Users
        //user = new User();
        if (ds.child(userId).exists()) {
            /*user.setFullname(ds.child(userId).getValue(User.class).getFullname()); //set the full name
            user.setUsername(ds.child(userId).getValue(User.class).getUsername()); //set the username
            user.setProfilePicture(ds.child(userId).getValue(User.class).getProfilePicture()); //set the profile picture

            //Display Information
            Log.d("DataSnapchat Test", "ShowData Name: " + user.getFullname());
            Log.d("DataSnapchat Test", "ShowData Username: " + user.getUsername());
            Log.d("DataSnapchat Test", "ShowData Picture: " + user.getProfilePicture());

            ArrayList<String> userArrayList = new ArrayList<>();
            userArrayList.add(user.getFullname());
            userArrayList.add(user.getUsername());
            userArrayList.add(user.getProfilePicture());
            */

            String fullname = (String) ds.child(userId).child("Fullname").getValue();

            Toast.makeText(this, "Fullname: " + fullname, Toast.LENGTH_SHORT).show();

            //UserListAdapter adapter = new UserListAdapter(this, R.layout.find_profiles_search, userArrayList, mProfileSearch);
            //mListView.setAdapter(adapter);

            i++;
        }
    }
}

当我调试代码时,从不调用showData函数,也不调用Value Event Listener中的任何函数,这是异步问题吗?

When I debug the code the showData function is never called and neither functions within the Value Event Listener are called, is this an Async Problem?

目前,我正在尝试获取数据并将其插入变量(从字面上看,任何数据,只要我有一个有效的查询,就可以通过处理子字段等将其转换为适合的数据.)

Currently I am trying to fetch data and insert it into a variable (Literally any data, once I have a working query I can convert it to fit by manipulating child fields etc).

问题:addValueListener仅在数据库中的数据更改时才起作用吗?如果是这样,那有什么选择,如果不是,那么为什么这些功能不起作用.

Question: Does the addValueListener only work when data is changed in the database? If so then what is the alternative, if not then why are the functions not operating.

我没有收到任何错误,也没有任何内容记录到控制台.

I do not recieve any errors and nothing is logged to the console.

我想将数据库放入数组列表中,我知道我不应该使用getChildren来执行此操作,而在尝试获取所有数据之前,我要测试是否可以获取任何数据.

I want to put my database into an array list, I know that I shouldnt use getChildren to do this, put I am trying to test if I can get any data before I try and get all the data.

推荐答案

showData()中确实有一些怪异的循环.由于您要在特定用户的节点上附加ValueEventListener,因此您只需查找该特定用户的属性值即可:

You have some really weird loops in showData(). Since you're attaching a ValueEventListener to the node of the specific user, you can just look up the property values for that specific user:

myRef = mFirebaseDatabase.getReference().child("Users").child(userId);

myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.d("OnDataChange", dataSnapshot.getKey());
        Log.d("OnDataChange", dataSnapshot.child("Fullname").getValue(String.class));
    }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("Print","Cancelled Firebase: " + databaseError.toString());
    }
});

这篇关于Firebase侦听器跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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