How to update List<String> without adapter
Asked Answered
M

1

0

What I want is for every update I make in the firebase database to be reflected in the app too without completely restarting it

I previously used notifyDataStateChange in an app that contains a recyclerviewadapter, so when I made an update it would also be updated in the app

In this case, I don't have a recyclerview and I don't have an adapter, only a textview where I show the quotes when a button is pressed, so I don't know how I can update it.

Since I have used notifyDataStateChange in a previous app, I would like to use it again. If you have any other better solution, please let me know it isn't necessary to use notifyDataStateChange. I just want to update the data in my app.

HomeActivity.java // if u want to get the entier file refrence please let me know i will update the question

      TextView countTxt, quotesTxt, noAds;
      List<String> quotes_list;
      Model model;

       countTxt = findViewById(R.id.countText);
        quotesTxt = findViewById(R.id.quotesTextView);


databaseReference = FirebaseDatabase.getInstance().getReference("Quotes");
        model = new Model();
        quotes_list = new ArrayList<>();
        databaseReference.addValueEventListener(new ValueEventListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onDataChange(@NonNull @org.jetbrains.annotations.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());
            }

            @Override
            public void onCancelled(@NonNull @org.jetbrains.annotations.NotNull DatabaseError error) {
                Toast.makeText(HomeActivity.this, "Error", Toast.LENGTH_SHORT).show();
                Log.d(TAG, error.getMessage());

            }
        });

Model.java

package com.example.philosophicalquotes;

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;
    }
}
Mosenthal answered 18/6, 2022 at 5:23 Comment(4)
notifyDataStateChange is not a magic stick its just a method which happens to be written in adapter to notify about the data set change . You can create a method to update the UI i.e change the data on screen . and call this method of firebase data change callback .Ectosarc
if i know everything why do i ask, i searched for how to Update List<String> but didnt find a good answer that solves my issueMosenthal
There's already public void onDataChange(DataSnapshot snapshot).Bihar
yes but the data only change when i restartMosenthal
E
0

When you are doing changes at the Firebase Realtime Database, the data will update at the application if you are using addValueEventListener(ValueEventListener).

You can refer to this link. https://firebase.google.com/docs/reference/android/com/google/firebase/database/ValueEventListener#public-abstract-void-ondatachange-datasnapshot-snapshot

Refer to your question, you are using addValueEventListener. The value will update itself at the application.

I'm assuming this line code will print when you are creating new object.

Log.d(TAG, quotes_list.size().toString());
Emden answered 18/6, 2022 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.