Firebase查询

时间:2017-03-14 12:16:37

标签: android firebase-realtime-database android-recyclerview

我到处搜索,但我无法弄清楚这一点。 我理解嵌套查询是可能的,但我试图使用FirebaseRecyclerAdapter中的结果,它似乎只接受一个查询。

以下是JSON中数据库的结构,我需要知道如何查询特定位置附近的所有用户(基于'位置')并从'中检索他们的详细信息。用户'

{
  "user_location" : {
    "bqKoBzNSb8apOZxzGk2PL7hpFuU2" : {
      "lati" : 16.537221908569336,
      "longi" : 80.63591003417969,
      "timestamp" : "17:19:36"
    },
    "z6jjD4t0SjNrqR6EU3aU39vH5iB3" : {
      "lati" : 16.537214279174805,
      "longi" : 80.63591766357422,
      "timestamp" : "09:34:24"
    }
  },
  "users" : {
    "8t4sVwfFl1RuXAgzOTihc9D4YhM2" : {
      "age" : 18,
      "bloodType" : "A-",
      "email" : "zxcv@gmail.com",
      "phnum" : "9502524713",
      "username" : "zxcv"
    },
    "bqKoBzNSb8apOZxzGk2PL7hpFuU2" : {
      "age" : 0,
      "email" : "sckool@gmail.com",
      "username" : "sckool"
    },
    "iexFcGrrztc81D3xpui67tYuim22" : {
      "age" : 18,
      "bloodType" : "A-",
      "email" : "abcd@gmail.com",
      "phnum" : "9502452413",
      "username" : "abcd"
    },
    "z6jjD4t0SjNrqR6EU3aU39vH5iB3" : {
      "age" : 18,
      "email" : "qwer@gmail.com",
      "username" : "qwer"
    }
  },
  "users_active" : {
    "8t4sVwfFl1RuXAgzOTihc9D4YhM2" : {
      "age" : 18,
      "bloodType" : "A-",
      "email" : "zxcv@gmail.com",
      "phnum" : "9502524713",
      "username" : "zxcv"
    },
    "bqKoBzNSb8apOZxzGk2PL7hpFuU2" : {
      "age" : 0,
      "email" : "sckool@gmail.com",
      "username" : "sckool"
    },
    "iexFcGrrztc81D3xpui67tYuim22" : {
      "age" : 18,
      "bloodType" : "A-",
      "email" : "abcd@gmail.com",
      "phnum" : "9502452413",
      "username" : "abcd"
    },
    "z6jjD4t0SjNrqR6EU3aU39vH5iB3" : {
      "age" : 0,
      "email" : "qwer@gmail.com",
      "username" : "qwer"
    }
  }
}

以下代码创建RecyclerView

 tm=getTree();

    Query postsQuery = getQuery(mDatabase);

    mAdapter = new FirebaseRecyclerAdapter<User, PostViewHolder>(User.class, R.layout.item_post,
            PostViewHolder.class, postsQuery) {

        @Override
        protected void populateViewHolder(final PostViewHolder viewHolder, final User model, final int position) {
            final DatabaseReference postRef = getRef(position);

            Log.d(TAG,"populateViewHolder");
            final String postKey = postRef.getKey();
            System.out.println(postKey);

            });

         // Bind Post to ViewHolder
                viewHolder.bindToPost(model, postKey, getTree());

        }


    };

    mRecycler.setAdapter(mAdapter);

我得到了下面代码的查询

@Override
public Query getQuery(DatabaseReference databaseReference) {
    final String myUserId = getUid();

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference ref = database.getReference("user_location");

    ref.orderByKey().equalTo(myUserId).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
            myloc = dataSnapshot.getValue(Locat.class);
            latFrom=myloc.lati;
            lngFrom=myloc.longi;
           }});

        ref.orderByKey().addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
            otherloc = dataSnapshot.getValue(Locat.class);
            latTo = otherloc.lati;
            lngTo = otherloc.longi;
            dist = getDistanceInfo();

            if ( dist<10 && dataSnapshot.getKey()!=myUserId ) {

                tm.put(dist, dataSnapshot.getKey());

            }
        }

    });


    Query donors= databaseReference.child("users").orderByKey();

    return donors;
 }

我来自FirebaserecyclerAdapter&#39;中的用户查询并让“附近的所有用户”加入“getQuery&#39;并将它们放在一个TreeMap&#39;中,但我如何检查每个键对着Map中的键?

0 个答案:

没有答案