I am creating a journal app. I am trying to fetch the current users journal entries and then convert that into a journal object which will be put into a journal list. Eventually, this journal list will be sent to the RecyclerView Adapter.
I have this piece of code in onCreate()
:
myCollection.whereEqualTo("userId", userId).addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable @org.jetbrains.annotations.Nullable QuerySnapshot value, @Nullable @org.jetbrains.annotations.Nullable FirebaseFirestoreException error) {
for(QueryDocumentSnapshot snapshot : value){
Journal myJournal = snapshot.toObject(Journal.class);
journals.add(myJournal);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(journals, JournalList.this);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(JournalList.this));
myAdapter.notifyDataSetChanged();
}
}
});
If I move this part:
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(journals, JournalList.this);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(JournalList.this));
myAdapter.notifyDataSetChanged();
out of the onEvent
block(but still in onCreate() ), the journal is still sent to firebase, but it seems like the RecyclerViewAdapter isn't invoked until I add the second post.
My guess is that either Android Studio skips over the onEvent() block and continues on(possibly puts it in a queue considering it knows it will take time to execute), or it runs on a background thread in which the adapter part finishes first. Either way, an empty arrayList of journals is sent to Firestore.
However, I'm unsure which one of these scenarios is actually occurring. If someone could confirm, I would appreciate it. Thanks.
Update: Code that doesn't work:
myCollection.whereEqualTo("userId", userId).addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable @org.jetbrains.annotations.Nullable QuerySnapshot value, @Nullable @org.jetbrains.annotations.Nullable FirebaseFirestoreException error) {
for(QueryDocumentSnapshot snapshot : value){
Journal myJournal = snapshot.toObject(Journal.class);
journals.add(myJournal);
}
}
});
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(journals, JournalList.this);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(JournalList.this));
myAdapter.notifyDataSetChanged();
Full code of JournalList.java(if needed):
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.List;
public class JournalList extends AppCompatActivity {
private RecyclerView recyclerView;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference myCollection = db.collection("Journal");
private FirebaseAuth firebaseAuth;
private FirebaseUser currentUser;
private List<Journal> journals;
private Toolbar myToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_journal_list);
recyclerView = findViewById(R.id.recyclerView);
firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();
String userId = currentUser.getUid();
myToolbar = findViewById(R.id.my_toolbar);
journals = new ArrayList<>();
setSupportActionBar(myToolbar);
myCollection.whereEqualTo("userId", userId).addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable @org.jetbrains.annotations.Nullable QuerySnapshot value, @Nullable @org.jetbrains.annotations.Nullable FirebaseFirestoreException error) {
for(QueryDocumentSnapshot snapshot : value){
Journal myJournal = snapshot.toObject(Journal.class);
journals.add(myJournal);
}
}
});
//my guess:
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(journals, JournalList.this);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(JournalList.this));
myAdapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.addNote:
startActivity(new Intent(JournalList.this, PostJournalActivity.class));
break;
case R.id.sign_out:
firebaseAuth.signOut();
startActivity(new Intent(JournalList.this, MainActivity.class));
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}