我正在将firebase realtime database
中的某些项目提取到firebaseRecyclerView.
中,正在使用此代码使数据在我的application
模块中脱机显示。
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
我还有一个spinner
,其中装有一些物品。我要的是单击这些项目中的任何一项,recyclerview
项都应该动态地自动排序。我试图通过查询数据库项来做到这一点,但是没有用。在我看来,query
参数不适用于离线数据。
我还能尝试什么?
private RecyclerView mRecyclerView;
private DatabaseReference mDatabase;
private ProgressBar mProgressBar;
private Spinner spinner;
private Query query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//populating spinner
spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.AllEpisodes, R.layout.custom_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
mProgressBar = findViewById(R.id.progressBarMain);
//setting database and recycler view
mDatabase = FirebaseDatabase.getInstance().getReference().child("Global");
mDatabase.keepSynced(true);
mRecyclerView = findViewById(R.id.main_recyclerview);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStart() {
super.onStart();
query = mDatabase;
final FirebaseRecyclerAdapter<Sermon, SermonViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Sermon, SermonViewHolder>(Sermon.class, R.layout.item_view, SermonViewHolder.class,query) {
@Override
protected void populateViewHolder(SermonViewHolder viewHolder, final Sermon model, int position) {
mProgressBar.setVisibility(View.INVISIBLE);
}
};
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
query=mDatabase.limitToLast(1);
firebaseRecyclerAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
break;
case 1:
query=mDatabase.limitToLast(2);
firebaseRecyclerAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
break;
case 2:
query=mDatabase.limitToLast(3);
firebaseRecyclerAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
break;
case 3:
query=mDatabase.limitToLast(4);
firebaseRecyclerAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
break;
case 4:
query=mDatabase.limitToLast(5);
firebaseRecyclerAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
break;
case 5:
query=mDatabase.limitToLast(6);
firebaseRecyclerAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
break;
case 6:
query=mDatabase.limitToLast(7);
firebaseRecyclerAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
break;
case 7:
query=mDatabase.limitToLast(8);
firebaseRecyclerAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
break;
case 8:
query=mDatabase.limitToLast(9);
firebaseRecyclerAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}