Search code examples
androidfirebaseandroid-recyclerviewnotifydatasetchangedfirebaseui

Does anyone know if Firebase can notify the adapter against a Query.


Can I notify the adapter to change the query? As now it does nothing... anyone?

05-28 17:04:07.358 15585-15674/com.example.rasmusjosefsson.rjcar D/OpenGLRenderer: endAllStagingAnimators on 0x7ff46e405c00 (ListPopupWindow$DropDownListView) with handle 0x7ff46f3d1b00

onCreate()

private DatabaseReference mUserRef;
private String mTravelTypeSelected;
private Query queryRefSelected;
private FirebaseRecyclerAdapter<MyWaypoint, LatLngViewHolder> mFirebaseRecyclerViewAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    //*************** TRAVEL SPINNER**************//
    mSpinnerTravelType = (Spinner) findViewById(R.id.spinner_nav);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.traveltype_array, R.layout.spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    mSpinnerTravelType.setAdapter(adapter);

    mSpinnerTravelType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            if (position == 0) {
                queryRefSelected = mUserRef;
            } else if (position == 1) {
                queryRefSelected = mUserRef.orderByChild("travelType").equalTo("Work");
                mTravelTypeSelected = "Work";
                Log.i("TEST", position + " ");

            } else if (position == 2) {
                queryRefSelected = mUserRef.orderByChild("travelType").equalTo("Private");
                Log.i("TEST", position + " ");
            }
            mFirebaseRecyclerViewAdapter.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
    recyclerView.setHasFixedSize(false);
    LinearLayoutManager manager = new LinearLayoutManager(this);
    manager.setReverseLayout(false);
    recyclerView.setLayoutManager(manager);
}

RecyclerViewAdapter

private void attachRecyclerViewAdapter() {

    mFirebaseRecyclerViewAdapter = new FirebaseRecyclerAdapter<MyWaypoint, LatLngViewHolder>(MyWaypoint.class, R.layout.list_item, LatLngViewHolder.class, queryRefSelected) {
        @Override
        protected void populateViewHolder(final LatLngViewHolder latLngViewHolder, MyWaypoint item, final int i) {

            Log.i("SELECTED INSIDE POP", queryRefSelected.toString());

            String locA = item.getOrigin();
            latLngViewHolder.locationA.setText(locA);

            String locB = item.getDestination();
            latLngViewHolder.locationB.setText(locB);

            String distance = item.getDistance();
            latLngViewHolder.distance.setText(distance);

           final String duration = item.getDuration();

        }
    };
    recyclerView.setAdapter(mFirebaseRecyclerViewAdapter);    
}

onStart

@Override
public void onStart() {
    super.onStart();
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {

        // User is signed in
        String mUserId = user.getUid();

        // String userUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/waypoints";
        String userUrl = "https://rjcar3.firebaseio.com/users/lP1K8Ik0zrhaXD6AR6C0x7ozi5g2/waypoints";
        mUserRef = FirebaseDatabase.getInstance().getReferenceFromUrl(userUrl);

        // Set standard query
        queryRefSelected = mUserRef;

        // attaching the adapter
        attachRecyclerViewAdapter();
    }
}

Solution

  • You are calling mFirebaseRecyclerViewAdapter.notifyDataSetChanged() which will only tell the RecyclerView to refresh the views it already has.

    If you want to change the Adapter's underlying query, you will need to call attachRecyclerViewAdapter() again to re-create the adapter with a new Query. If you do this, make sure to call mFirebaseRecyclerViewAdapter.cleanup() on the old adapter.