gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:36:39 25 4
gpt4 key购买 nike

我有一个绑定(bind)到 FirebaseRecyclerAdapterRecyclerView。我需要根据用户使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com