简体   繁体   中英

Filter the query and refresh the recycler view using firebase in android

@Override
protected void onStart() {
    super.onStart();

    mAuth.addAuthStateListener(mAuthListener);

    FirebaseRecyclerAdapter<Product, ProductViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Product, ProductViewHolder>(

            Product.class,
            R.layout.product_row,
            ProductViewHolder.class,
            mDatabase

    ) {
        @Override
        protected void populateViewHolder(ProductViewHolder viewHolder, Product model, int position) {

            Log.d(TAG, "loading view " + position);
            final String product_id = getRef(position).getKey();
            viewHolder.setTitle(model.getName());
            viewHolder.setDesc(model.getDesc());
            viewHolder.setImage(getApplicationContext(), model.getImage());
            viewHolder.setUsername(model.getUsername());

            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent productDetailIntent = new Intent();
                    productDetailIntent.setClass(MainActivity.this, ProductDetailActivity.class);
                    productDetailIntent.putExtra("product_id", product_id);
                    Log.d(TAG + " product_id", product_id);
                    startActivity(productDetailIntent);
                }
            });

            Log.d(TAG, "finish loading view");
        }
    };

    mProductList.setAdapter(firebaseRecyclerAdapter);
}

The above is the code that I use to get the data from the firebase realtime database. How can I change the query (mDatabase in my code) and refresh the recyclerview to filter the data? How to set a new adapter? Please give me some helps.Thank you.

Try this:

FirebaseRecyclerAdapter<Product, ProductViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Product, ProductViewHolder>(

        Product.class,
        R.layout.product_row,
        ProductViewHolder.class,
        mDatabase.orderByChild("KeyName").equalTo("Value") //add this line 
)
 mProductList.setAdapter(firebaseRecyclerAdapter);
  firebaseRecyclerAdapter.notifyDataSetChanged();  // also add this line

Set query in your database...

FirebaseRecyclerAdapter<Product, ProductViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Product, ProductViewHolder>(

            Product.class,
            R.layout.product_row,
            ProductViewHolder.class,
            mDatabase.orderByChild("KeyName").equalTo("Value")

    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM