0

I have 2000 quotes in Firebase. I have two buttons, first & latest. As the name suggests, when I click 'first', it shows the first data from Firebase in the logs, and the same for the 'latest', when I click it, it gets and shows the latest data from Firebase in the logs.

The issue is when I try to setText, it shows a random quote and I don't know what I should do. I asked this previous question about how to get the first and last data from Firebase, and I received an answer too, but the issue is I'm not able to setText the correct data.

private void latestQuote() {
    DatabaseReference db = FirebaseDatabase.getInstance().getReference();
    DatabaseReference quotesRef = db.child("Quotes");
    Query queryForFirstElement = quotesRef.orderByKey().limitToLast(1); //
    queryForFirstElement.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DataSnapshot> task) {
            if (task.isSuccessful()) {
                for (DataSnapshot qs : task.getResult().getChildren()) {
                    String title = qs.child("title").getValue(String.class);
                    Log.d("TAG", title);
                    quotes_list.add(title);
                    position = randomQ.nextInt(quotes_list.size());
                }
                quotesTxt.setText(quotes_list.get(position));
                countTxt.setText(position + "/" + quotes_list.size());
            } else {
                Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
            }
        }
    });
}

private void firstQuote() {
    DatabaseReference db = FirebaseDatabase.getInstance().getReference();
    DatabaseReference quotesRef = db.child("Quotes");
    Query queryForFirstElement = quotesRef.orderByKey().limitToFirst(1); //
    queryForFirstElement.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DataSnapshot> task) {
            if (task.isSuccessful()) {
                for (DataSnapshot qs : task.getResult().getChildren()) {
                    String title = qs.child("title").getValue(String.class);
                    Log.d("TAG", title);
                    quotes_list.add(title);
                    position = randomQ.nextInt(quotes_list.size());
                }
                quotesTxt.setText(quotes_list.get(position));
                countTxt.setText(position + "/" + quotes_list.size());
            } else {
                Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
            }
        }
    });

}

I'm pretty sure this is happening because of this line position = randomQ.nextInt(quotes_list.size());, but even if I remove it, it just doesn't set any text in the text view, but still shows the expected data in the logs.

Here is an example on how I'm getting some other data and this method just works perfectly:

@SuppressLint("SetTextI18n")
private void back() {  
    if (position > 0) {
        position = (position - 1) % quotes_list.size();
        quotesTxt.setText(quotes_list.get(position));
        countTxt.setText(position + "/" + quotes_list.size());
    }
}

@SuppressLint("SetTextI18n")
private void next() {
    position = (position + 1) % quotes_list.size();
    quotesTxt.setText(quotes_list.get(position));
    countTxt.setText(position + "/" + quotes_list.size());
}

@SuppressLint("SetTextI18n")
private void random() {
    position = randomQ.nextInt(quotes_list.size());
    quotesTxt.setText(quotes_list.get(position));
    countTxt.setText(position + "/" + quotes_list.size());
}

Adding Model.class as requested in comments

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;
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

0 Answers0