After getting all documents in collection can't add field to ArrayList, Android Studio, Java, Firebase Firestore

Exception or error:

Hi guys I have a problem as I am getting the whole collection from Firebase Firestore and want get the field “topic name” in each document and add it into an ArrayList to then display this topics in a ListView. It is gonna be a part of Forum part of my application.

BUT!

It seems that it gets the documents, and reads their fields as I can see in the Logs

Log.d(TAG, document.getId() + " => " + document.get("topic_name"));

_

D/TAG: 6EvC4SqqDxR4TYelLOKM => Topic 1
KpXLBDzSkAb2u96knG8L => Topic 4
SY49j4RqcOI1v79EIVME => Topic 3
xS0Hw4Rj5KPGj1G7vy7T => Topic 2

it doesn’t add it to the ArrayList or it is in some way gone because the device do not display the list.

At First I thought it is because the ArrayList when it is accessed from within inner class needs to be declared final but that is not the case, as I added a test topics to check that assumption. (Test Topic 123-Test to Topic 125) and it displays them normally, so that is not the case with ArrayList being final.

I tried putting the whole code responsible for ListView inside the onComplete method but it doesn’t seem to work, because the Adapter becomes full of errors, I would like some help if possible or maybe an alternate idea on how to do it. If I replicated the topic I am sorry I tried looking for answer but was unable to find it.

The code is below:

public class Forum extends Activity {

public static final String TAG = "TAG";
FirebaseFirestore db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forum);


    db = FirebaseFirestore.getInstance();
    final ArrayList<String> forumTopics = new ArrayList<>();

    db.collection("forum_topics")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {

                    if (task.isSuccessful()) {
                        int count = 0;
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            forumTopics.add(count, document.getString("topic_name"));
                            count += 1;
                            Log.d(TAG, document.getId() + " => " + document.get("topic_name"));
                        }

                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

    String test_string = "Test Topic 123";
    forumTopics.add(0,test_string);
    forumTopics.add(1,"Test Topic 124");
    forumTopics.add(2,"Test Topic 125");
    forumTopics.add(3,"Test Topic 126");

    ListAdapter forumAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, forumTopics);
    ListView forumListView =  findViewById(R.id.ForumListView);
    forumListView.setAdapter(forumAdapter);

    forumListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String topic = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(Forum.this, topic, Toast.LENGTH_LONG).show();
                }
            }
    );
}

This is the output on the device

Thank you very much.

How to solve:

Try with this

db.collection("forum_topics")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {

                    if (task.isSuccessful()) {
                        int count = 0;
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            forumTopics.add(count, document.getString("topic_name"));
                            count += 1;
                             forumTopics.add(document.get("topic_name"));
                        }
                        forumAdapter.notifyDataSetChanged()
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

    ListAdapter forumAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, forumTopics);
    ListView forumListView =  findViewById(R.id.ForumListView);
    forumListView.setAdapter(forumAdapter);

###

I was able to solve the problem with your help below is the solution that worked in my case. Thank you everyone that helped me I am very grateful and may you have wonderful day.

    db = FirebaseFirestore.getInstance();
    final ArrayList<String> forumTopics = new ArrayList<>();

    final ListAdapter forumAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, forumTopics);
    ListView forumListView =  findViewById(R.id.ForumListView);
    forumListView.setAdapter(forumAdapter);

    db.collection("forum_topics")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {

                    if (task.isSuccessful()) {
                        int count = 0;
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            forumTopics.add(count, document.getString("topic_name"));
                            count += 1;
                        }
                        ((ArrayAdapter) forumAdapter).notifyDataSetChanged();
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

    forumListView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String topic = String.valueOf(parent.getItemAtPosition(position));
                    Toast.makeText(Forum.this, topic, Toast.LENGTH_LONG).show();
                }
            }
    );

Leave a Reply

Your email address will not be published. Required fields are marked *