How to update textviews when data is updated in firebase

Viewed 134

I have a quotes app, and what I do is I store new quotes in excel and then convert them to JSON (as well as the old ones), and then import the json file into Firebase.

Now the problem is when I do this if my app is already running and I request a new quote to display, the total number of quotes (for example 1000) gets confusing. It doesn't display the correct total number unless I completely close my app and then reopen it.

Would you please tell me how to update both texts (1. The total number 2. the textview showing the quotes) with the updated quotes, when an event occurs (e.g., clicking the nav buttons to bring next or previous quotes or like getting on resume)?

Look at this

(the text changed itself to 10001 when i just added one quote in the firebase and even the added quote is not displaying when i press the latest quote - which get the lastest added quote in firebase)

Im using this right now to get data

databaseReference = FirebaseDatabase.getInstance().getReference("Quotes");
        model = new Model();
        quotes_list = new ArrayList<>();
        databaseReference.addValueEventListener(new ValueEventListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot1 : snapshot.getChildren()) {
                    model = dataSnapshot1.getValue(Model.class);
                    if (model != null) {
                        quotes_list.add(model.getTitle());
                        position = randomQ.nextInt(quotes_list.size());
                        Log.d(TAG, quotes_list.toString());

                    }
                }
                quotesTxt.setText(quotes_list.get(position));
                countTxt.setText(position + "/" + quotes_list.size());
                Log.d(TAG, quotes_list.toString());
            }

Model

package com.example.philosophicalquotes.data;

public class Model {
    String title;

    public Model() {
    }

    public Model(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
1 Answers

Whenever your onDataChange is called, it gets a DataSnapshot with all data at the Quotes path in your database. You need to clear the existing data from the quotes_list list before rebuilding it.

databaseReference.addValueEventListener(new ValueEventListener() {
    @SuppressLint("SetTextI18n")
    @Override
    public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
        quotes_list.clear(); // 
        for (DataSnapshot dataSnapshot1 : snapshot.getChildren()) {
            model = dataSnapshot1.getValue(Model.class);
            if (model != null) {
                quotes_list.add(model.getTitle());
                position = randomQ.nextInt(quotes_list.size());
                Log.d(TAG, quotes_list.toString());
            }
        }
        quotesTxt.setText(quotes_list.get(position));
        countTxt.setText(position + "/" + quotes_list.size());
        Log.d(TAG, quotes_list.toString());
    }
Related