有谁知道 Firebase 是否可以针对查询通知适配器。
Does anyone know if Firebase can notify the adapter against a Query.
我可以通知适配器更改查询吗?现在它什么都不做......有人吗?
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();
}
}
您正在调用 mFirebaseRecyclerViewAdapter.notifyDataSetChanged()
,它只会告诉 RecyclerView 刷新它已有的视图。
如果您想更改适配器的基础查询,您将需要再次调用 attachRecyclerViewAdapter()
以使用新的 Query
重新创建适配器。如果这样做,请确保在旧适配器上调用 mFirebaseRecyclerViewAdapter.cleanup()
。
我可以通知适配器更改查询吗?现在它什么都不做......有人吗?
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();
}
}
您正在调用 mFirebaseRecyclerViewAdapter.notifyDataSetChanged()
,它只会告诉 RecyclerView 刷新它已有的视图。
如果您想更改适配器的基础查询,您将需要再次调用 attachRecyclerViewAdapter()
以使用新的 Query
重新创建适配器。如果这样做,请确保在旧适配器上调用 mFirebaseRecyclerViewAdapter.cleanup()
。