如何更新firebase回收器适配器的查询引用?

时间:2016-09-30 14:06:42

标签: android firebase firebase-realtime-database firebaseui

我有RecyclerView绑定到FirebaseRecyclerAdapter。我需要根据用户使用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){
     ???????
}
} 

2 个答案:

答案 0 :(得分:1)

找不到合适的答案,但作为临时解决方法,只需创建新适配器并将其设置为RecyclerView即可。仍然觉得应该有更优雅的方式来做到这一点。

答案 1 :(得分:0)

使用FirebaseRecyclerViewAdapter实现解决方案。或者将相同的代码更改合并到您的应用程序库中。

https://firebaseui.com/docs/android/index.html?com/firebase/ui/FirebaseRecyclerViewAdapter.html