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;
}
}
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 . – Ectosarcpublic void onDataChange(DataSnapshot snapshot)
. – Bihar