我有一個RecyclerView
綁定到FirebaseRecyclerAdapter
。我需要根據用戶使用spinner
設置的值更新顯示的數據。如何更新Firebase回收站適配器的查詢引用?
事情是這樣的:
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference ref = db.getReference("data");
Query query = ref.orderByChild("index").startAt(loadPreferences()).endAt(loadPreferences());
final Adapter fireAdapter = new Adapter(Model.class, R.layout.model_card, Adapter.FireViewHolder.class, query);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String selectedIndex = String.valueOf(adapterView.getItemAtPosition(i));
savePreferences(selectedIndex);
Query newQuery = ref.orderByChild("index").startAt(selectedIndex).endAt(selectedIndex));
fireAdapter.updateQuery(newQuery); //This is what i'm looking for
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
我的適配器:
class Adapter extends FirebaseRecyclerAdapter<Model, Adapter.FireViewHolder> {
Adapter(Class<Model> modelClass, int modelLayout, Class<FireViewHolder> viewHolderClass, Query ref) {
super(modelClass, modelLayout, viewHolderClass, ref);
}
@Override
protected void populateViewHolder(FireViewHolder viewHolder, Model model, int position) {
viewHolder.text.setText(model.getText());
}
static class FireViewHolder extends RecyclerView.ViewHolder {
TextView text;
public FireViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.textView);
}
}
public void updateQuery(Query newQuery){
???????
}
}
,你會是最偉大的,分享你如何圍繞優化了你的方法是什麼? – Alexander