In my app there is an activity that when it opens it should display data in a RecyclerView. The data is stored in a collection in Firestore but unfortunately it is only working for API 21, 22 and 23, everything above is not displaying the data (see screenshots). Also sometimes it is not showing the data on the first time the user opens the activity, only when he opens it a second time or logs out and in again (in API 21). Is this something that is known or is there something with my code? Or is there a better way to display the data of a collection in a RecyclerView?
I also noticed a strange behavior:
When I add an EditText to the layout without doing anything with it in the activity, when the user clicks on the EditText, the desired documents are then displayed as they should be. On the emulator running API 21, the data shows without having to click on the EditText, so the mentioned behavior applies for API greater 24. Can someone explain this behavior?
Any help is much appreciated!
I followed this tutorial where everything works fine:
https://www.youtube.com/watch?v=lAGI6jGS4vs
My app only targets API starting at 21.
Here is my activity with the RecyclerView:
public class PrizeGameActivity extends AppCompatActivity {
private Toolbar mToolbar;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference prizesRef = db.collection("Prizes");
private PrizeGameAdapter prizeGameAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prize_game);
// Sets the custom toolbar as ActionBar
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setUpRecyclerView();
}
private void setUpRecyclerView() {
Query query = prizesRef.orderBy("text",Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Prizes> options = new FirestoreRecyclerOptions.Builder<Prizes>()
.setQuery(query, Prizes.class)
.build();
prizeGameAdapter = new PrizeGameAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view_prize_game);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(prizeGameAdapter);
prizeGameAdapter.startListening();
}
@Override
protected void onStart() {
super.onStart();
if(prizeGameAdapter != null)
{
prizeGameAdapter.startListening();
}
}
@Override
protected void onStop() {
super.onStop();
if(prizeGameAdapter != null)
{
prizeGameAdapter.startListening();
}
}
// Makes a left to right animation when user clicks the back button of the toolbar
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
// Makes a right left animation when clicking the back button
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
Here is my Adapter:
public class PrizeGameAdapter extends FirestoreRecyclerAdapter<Prizes, PrizeGameAdapter.PrizeGameHolder> {
public PrizeGameAdapter(@NonNull FirestoreRecyclerOptions<Prizes> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull PrizeGameHolder holder, int position, @NonNull Prizes model) {
holder.textViewPrizeGame.setText(model.getText());
}
@NonNull
@Override
public PrizeGameHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_prize_game,parent, false);
return new PrizeGameHolder(view);
}
class PrizeGameHolder extends RecyclerView.ViewHolder {
TextView textViewPrizeGame;
public PrizeGameHolder(@NonNull View itemView) {
super(itemView);
textViewPrizeGame = itemView.findViewById(R.id.listitem_text_view_prize_game);
}
}
}
And here is my prizes class:
public class Prizes {
public Prizes (){
}
public String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Prizes(String text) {
this.text = text;
}
}