更改查询后,如何使用FirebaseUI刷新FirebaseAdapter中的数据?

时间:2018-09-11 13:00:09

标签: android firebase firebase-realtime-database

我想允许用户对数据进行排序和过滤。我具有Firebase Adapter的此实现,并且一切正常。当我想使用例如过滤数据时价格,RecyclerView中的数据不变。您能帮忙,说一下哪里出了问题吗?

这是查询-取决于从Spinner中选择的用户:

Query query;

if (!priceLower.equals("Any")) {    // option from Spinner
    query = FirebaseDatabase.getInstance()
            .getReference()
            .child("Students")
            .orderByChild("price")
            .startAt(priceLower)
            .limitToLast(50);
} else if (!priceHigher.equals("Any")) {
    query = FirebaseDatabase.getInstance()
            .getReference()
            .child("Students")
            .orderByChild("price")
            .endAt(priceHigher)
            .limitToLast(50);
} else {
    query = FirebaseDatabase.getInstance()
            .getReference()
            .child("Students")
            .orderByChild("price")
            .limitToLast(50);
}

getData(query);

这是从Firebase获取数据的方法:

 public void getData(Query query) {
        FirebaseRecyclerOptions<Students> firebaseOptions =
                new FirebaseRecyclerOptions.Builder<Students>()
                        .setQuery(query, Students.class)
                        .build();

        FirebaseAdapter = new FirebaseRecyclerAdapter<Students, StudentsHolder>(firebaseOptions) {
            @NonNull
            @Override
            public StudentsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_students, parent, false);
                return new StudentsHolder(view);
            }
            @Override
            protected void onBindViewHolder(@NonNull StudentsHolder holder, int position, @NonNull final Students student) {
                holder.setName(student.getName());
                holder.setPrice(student.getPrice());

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       // ...
                    }
                });
            }
            @Override
            public void onDataChanged() {       
                progressDialog.setVisibility(View.GONE);
                mRecyclerView.setAdapter(FirebaseAdapter);    
            }
        };
    }

2 个答案:

答案 0 :(得分:1)

要解决此问题,您应该在getData()方法的末尾添加以下代码行:

FirebaseAdapter.startListening();

这意味着每次更改查询时,都将创建并设置一个新的适配器,并且还开始侦听更改。

答案 1 :(得分:0)

@Override
        public void onDataChanged() {       
            progressDialog.setVisibility(View.GONE);
            mRecyclerView.setAdapter(FirebaseAdapter);    
        }

仅当更改Firebase数据库上的数据时才调用此方法。但是根据您的要求,这些数据保持不变,只是您所应用的过滤器或排序方法正在更改。因此,永远不会调用此方法,并且firebaseadapter将仅反映原始查询。

尝试一下:

   public void getData(Query query) {
    FirebaseRecyclerOptions<Students> firebaseOptions =
            new FirebaseRecyclerOptions.Builder<Students>()
                    .setQuery(query, Students.class)
                    .build();

    FirebaseAdapter = new FirebaseRecyclerAdapter<Students, StudentsHolder>(firebaseOptions) {
        @NonNull
        @Override
        public StudentsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_students, parent, false);
            return new StudentsHolder(view);
        }
        @Override
        protected void onBindViewHolder(@NonNull StudentsHolder holder, int position, @NonNull final Students student) {
            holder.setName(student.getName());
            holder.setPrice(student.getPrice());

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   // ...
                }
            });
        }
        @Override
        public void onDataChanged() {       

        }
    };

     progressDialog.setVisibility(View.GONE);
     mRecyclerView.setAdapter(FirebaseAdapter);   
}
每当更改过滤条件时,应调用

getData()方法。