2016-09-30 70 views
4

我有一個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){ 
    ??????? 
} 
} 

回答

0

找不到一個合適的答案,但作爲一個臨時的解決方法只是創造一個新的適配器,並將其設置爲RecyclerView作品。仍然覺得應該有一個更優雅的方式來做到這一點。

+0

,你會是最偉大的,分享你如何圍繞優化了你的方法是什麼? – Alexander

相關問題