作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个绑定(bind)到 FirebaseRecyclerAdapter
的 RecyclerView
。我需要根据用户使用 spinner
设置的值更新显示的数据。
像这样:
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){
???????
}
}
最佳答案
找不到合适的答案,但作为临时解决方法,只需创建一个新适配器并将其设置为 RecyclerView
即可。仍然觉得应该有一种更优雅的方式来做到这一点。
关于android - 如何更新 firebase 回收器适配器的查询引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39793436/
我是一名优秀的程序员,十分优秀!