从Firebase数据库中检索交叉对象子项

时间:2018-04-28 18:11:31

标签: android firebase firebase-realtime-database

我想weight quantityCat1item_namedatebranchCat2"Cat1" : { "-LBBWypuYLVn-aDYWK-N" : { "branch" : "b1", "date" : "28/Apr/2018", "weight" : "46", "item_name" : "item1", "quantity" : "20" }, "-LBBZKUlEJB-HdiKftoj" : { "branch" : "b2", "date" : "28/Apr/2018", "weight" : "112", "item_name" : "item2", "quantity" : "16" } }, "Cat2" : { "-LBBWQcC3acYk-OOiX3T" : { "branch" : "b1", "date" : "28/Apr/2018", "item_name" : "item1", "weight" : "2.7", "quantity" : "20" } } 中相同在Firebase实时数据库中。但我不知道如何检查条件。

JSON

mFishQuery = mFishRef.orderByChild("date").equalTo(mawStock.getDate());
//        mFishQuery = mFishRef.orderByKey();//.equalTo(mawStock.getDate());
//        Query fishWeight = mFishQuery.orderByChild("item_name").equalTo(mawStock.getItem_name()).getRef().orderByChild("quantity").equalTo(mawStock.getQuantity());
mFishQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        mWeightF = (Double) dataSnapshot.child("fish_weight").getValue();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

mFishRef.child("date").equalTo(mawStock.getDate()).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

到目前为止,我已完成以下工作:

<form action="#" onsubmit="javascript:window.open('{% url 'index' %}'+this.days.value,'null');return false">
        <input type="number" name="days" autofocus  required><br>
    </form>

3 个答案:

答案 0 :(得分:1)

尝试以下方法:

$('.k-klas div:child button').click(function(){
 $('#edit-opleidingsdatum').val($(this).closest('div').html("")
});

当您进行查询时,您只能使用一个孩子DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Cat2"); ref.orderByChild("item_name").equalTo("item1").addSingleValueEventListener(new ValueEventListener(){ @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot datas: dataSnapshot.getChildren()){ String date=datas.child("date").getValue().toString(); DatabaseReference refer=FirebaseDatabase.getInstance().getReference().child("Cat1"); refer.orderByChild("date").equalTo(date).addSingleValueEventListener(new ValueEventListener(){ @Override public void onDataChange(DataSnapshot dataSnapshot) { //retrieve data } @Override public void onCancelled(FirebaseError firebaseError) { } }); } @Override public void onCancelled(FirebaseError firebaseError) { } }); orderByChild()

所以,在这里你有了孩子orderByKey()的数据快照,然后你查询Cat2并从数据库中检索where item_name=item1

然后添加嵌套侦听器并执行查询date,并检索Cat1的数据。

答案 1 :(得分:0)

基本上,你只需要做足够的itr.next(),直到迭代器位于第n个位置(其中n是来自nextInt()的随机数),然后你就可以得到对象你想要getValue(),下面的例子应该很好地显示:

Sample data

获取随机生成密钥的解决方案。

questionsRef = FirebaseDatabase.getInstance().getReference().child("questions").child("DE");

questionsRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int questionCount = (int) dataSnapshot.getChildrenCount();
        int rand = random.nextInt(questionCount);
        Iterator itr = dataSnapshot.getChildren().iterator();

        for(int i = 0; i < rand; i++) {
            itr.next();
        }
        childSnapshot = (DataSnapshot) itr.next();
        question = childSnapshot.getValue(Question.class);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

答案 2 :(得分:0)

最后,我在@Peter Hadad答案的帮助下解决了问题,并在答案中作了一些修改。

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Cat1");
ref.orderByChild("item_name").equalTo("item1").addSingleValueEventListener(new ValueEventListener(){
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for(DataSnapshot datas : dataSnapshot.getChildren()){

        DatabaseReference refer=FirebaseDatabase.getInstance().getReference().child("Cat1");
        refer.orderByChild("date").equalTo(date).addSingleValueEventListener(new ValueEventListener(){

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (dataSnapshot.exists()) {

                    for (Datasnapshot ds : dataSnapshot) {

                        List<Datasnapshot> list = new ArrayList<>();
                        list.add(ds);

                        //compare data and apply logic

                    }

                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});