1

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();
    }
}
Rasmus Rajje Josefsson
  • 1,564
  • 2
  • 15
  • 33

1 Answers1

2

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.

Sam Stern
  • 24,624
  • 13
  • 93
  • 124
  • ohh i see, Thanks a lot! – Rasmus Rajje Josefsson May 31 '16 at 19:45
  • Offtopic: Is it possible to combine two queries? Month selected(12 items) and travel type(2items). Or is it better to store it different in the database, a node for private and one for work? – Rasmus Rajje Josefsson May 31 '16 at 20:21
  • You can only use one Query, it's always better to make your data in the database look similar to how you will want to query it rather than trying to do complex things on the client side. It will reduce the amount you have to download and make your app faster. – Sam Stern May 31 '16 at 22:01
  • Hmm, as you say either I do the split up, and wont be able to get all items at once, or maybe I can do as Franks says at "2" here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Rasmus Rajje Josefsson May 31 '16 at 22:09
  • You can simply call recreate(), to refresh the view of whole activity. – Rajbir Shienh Mar 31 '19 at 15:22