有谁知道Firebase是否可以通过查询通知适配器。

时间:2016-05-29 22:00:49

标签: android firebase android-recyclerview notifydatasetchanged firebaseui

我可以通知适配器更改查询吗?现在它什么也没做......有人吗?

  

05-28 17:04:07.358 15585-15674 / com.example.rasmusjosefsson.rjcar   D / OpenGLRenderer:0x7ff46e405c00上的endAllStagingAnimators   (ListPopupWindow $ DropDownListView),句柄为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();
    }
}

1 个答案:

答案 0 :(得分:2)

您正在调用mFirebaseRecyclerViewAdapter.notifyDataSetChanged(),它只会告诉RecyclerView刷新它已有的视图。

如果要更改Adapter的基础查询,则需要再次调用attachRecyclerViewAdapter()以使用新的Query重新创建适配器。如果这样做,请务必在旧适配器上调用mFirebaseRecyclerViewAdapter.cleanup()