addListenerForSingleValueEvent没有响应

时间:2018-10-24 15:55:29

标签: android firebase-realtime-database

query.addListenerForSingleValueEvent(new ValueEventListener()) 里面的功能不起作用。 如果在Firebase Db中未找到查询,则它应获取新查询并以未找到文本搜索查询的形式显示。

但是里面所有的东西都不起作用

TextView search_text=findViewById(R.id.search_text);
        if(search_query!=null)
        query = FirebaseDatabase.getInstance()
               .getReference(Common.STR_WALLPAPER)
               .orderByChild("imageName")
               .startAt(search_query).endAt(search_query +"\uf8ff");
        else query = FirebaseDatabase.getInstance()
                    .getReference(Common.STR_WALLPAPER)
                    .orderByChild("imageName");
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists())
                {
                    search_text.setVisibility(View.GONE);
                }
                else{
                    search_text.setVisibility(View.VISIBLE);
                    search_text.setText(search_query+" not found");
                    query = FirebaseDatabase.getInstance()
                            .getReference(Common.STR_WALLPAPER)
                            .orderByChild("imageName");
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
        options = new FirebaseRecyclerOptions.Builder<WallpaperItem>()
                .setQuery(query, WallpaperItem.class)
                .build();

addListenerForSingleValueEvent内部的函数没有响应。

1 个答案:

答案 0 :(得分:0)

TextView search_text=findViewById(R.id.search_text);
    if(search_query!=null)
    query = FirebaseDatabase.getInstance()
           .getReference(Common.STR_WALLPAPER)
           .orderByChild("imageName")
           .startAt(search_query).endAt(search_query +"\uf8ff");
    else query = FirebaseDatabase.getInstance()
                .getReference(Common.STR_WALLPAPER)
                .orderByChild("imageName");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists())
            {
                search_text.setVisibility(View.GONE);
            }
            else{
                search_text.setVisibility(View.VISIBLE);
                search_text.setText(search_query+" not found");
                query = FirebaseDatabase.getInstance()
                        .getReference(Common.STR_WALLPAPER)
                        .orderByChild("imageName");
            }
    options = new FirebaseRecyclerOptions.Builder<WallpaperItem>()
            .setQuery(query, WallpaperItem.class)
            .build();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

在addListenerForSingleValueEvent内部放置选项和适配器