FirebaseUI-Android icon indicating copy to clipboard operation
FirebaseUI-Android copied to clipboard

How to update the query at run time using the same adapter and in same fragment.

Open Princejack66 opened this issue 5 years ago • 2 comments

  • Android device: Android Studio
  • Android OS version: 4.5
  • Firebase/Play Services SDK version: 19.5.1
  • FirebaseUI version: 6.3.0

Step 3: Describe the problem:

I want to update the query at run time using the same adapter. I have a number of filters that I want to apply at run time to filter out the data from the real-time database. I have even tried the update option but that didn't work well and nothing happened.

Even the logcat don't show any error or bugs,

here is my code to update the query. Query to update

Query newQuery = FirebaseDatabase.getInstance().getReference().child("King").orderByChild("Category").equalTo("Gaming");
      FirebaseRecyclerOptions<ToolsInfo> newOption = new FirebaseRecyclerOptions.Builder<ToolsInfo>().setQuery(newQuery,ToolsInfo.class).build();
      myAdapter.updateOptions(newOption);
      recyclerView.setAdapter(myAdapter);
      myAdapter.notifyDataSetChanged();

Adapter Code

package com.example.pjtools;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.squareup.picasso.Picasso;
public class MyAdapter extends FirebaseRecyclerAdapter<ToolsInfo,MyAdapter.myViewHolder> {
    LoadingDialog loadingDialog;
    public MyAdapter(@NonNull FirebaseRecyclerOptions<ToolsInfo> options, Activity activity) {
        super(options);
        loadingDialog = new LoadingDialog(activity);
        loadingDialog.startLoading();
    }

    @Override
    protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull ToolsInfo model) {
        holder.name.setText(model.getName());
        holder.date.setText("Release Date: " + model.getRelease_date());
        holder.version.setText("Version: " + model.getVersion());
        holder.category.setText("Category: " + model.getCategory());
        Picasso.get().load(model.getImageURL()).fit().into(holder.img);

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(holder.cardView.getContext(),ToolOpenActivity.class);
                intent.putExtra("id",model.getId());
                holder.cardView.getContext().startActivity(intent);
            }
        });
    }

    @NonNull
    @Override
    public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row,parent,false);
        loadingDialog.stopLoading();
        return new myViewHolder(view);
    }

    class myViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView name, date, version, category;
        CardView cardView;
        public myViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.row_img);
            name = itemView.findViewById(R.id.row_name);
            date = itemView.findViewById(R.id.row_release_date);
            version = itemView.findViewById(R.id.row_version);
            category = itemView.findViewById(R.id.row_category);
            cardView = itemView.findViewById(R.id.cardView);


        }
    }

}

Princejack66 avatar Dec 11 '20 05:12 Princejack66

@Princejack66 can you describe more specifically what's not working when you call updateOptions? Does the data change at all? Do you see any messages in the logs?

samtstern avatar Dec 11 '20 11:12 samtstern

Not a single change all data disappeared and I haven't noticed anything in logcat like error or warning I mean I want to filter my data when my fragment starts this query executes query = FirebaseDatabase.getInstance().getReference().child("King"); All I want to do when the user clicks on the menu item data must be filter. And I want to change the query to this Query newQuery = FirebaseDatabase.getInstance().getReference().child("King").orderByChild("Category").equalTo("Gaming"); after clicking on the menu item but the above-given code isn't working and not showing a single data after selecting the menu item.

Princejack66 avatar Dec 11 '20 19:12 Princejack66