I am trying to get random data from firebase, but I am unsure how. I want to get random data in the following two ways:
when the app launches, it will pull any random quote from the list of 2000 quotes (maybe in the future the number can get upto 10000+ quotes).
now if the user clicks the button (random button) I want a new random data to be shown every time the user clicks the button
The database is like this
I didn't implement a method for getting random data from Firebase, because I don't know how to do it.
I tried to implement some answers, but didn't know how. Here is the code I tried, but I wasn't sure how to use it.
Random random = new Random();
int position = random.nextInt(quotes_list.size());
HomeActivity
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
TextView countTxt, quotesTxt;
ImageView previousBtn, randomBtn, shareBtn, copyBtn, nextBtn;
List<String> quotes_list;
DatabaseReference databaseReference;
Model model;
int position = 0;
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
countTxt = findViewById(R.id.countText);
quotesTxt = findViewById(R.id.quotesTextView);
previousBtn = findViewById(R.id.backBtn);
randomBtn = findViewById(R.id.randomBtn);
shareBtn = findViewById(R.id.shareBtn);
copyBtn = findViewById(R.id.copyBtn);
nextBtn = findViewById(R.id.nextBtn);
previousBtn.setOnClickListener(this);
randomBtn.setOnClickListener(this);
shareBtn.setOnClickListener(this);
copyBtn.setOnClickListener(this);
nextBtn.setOnClickListener(this);
Random random = new Random();
int position = random.nextInt(quotes_list.size());
databaseReference = FirebaseDatabase.getInstance().getReference("Quotes");
model = new Model();
quotes_list = new ArrayList<>();
databaseReference.addValueEventListener(new ValueEventListener() {
@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());
}
}
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
}
@Override
public void onCancelled(@NonNull @org.jetbrains.annotations.NotNull DatabaseError error) {
Toast.makeText(HomeActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
// Ads
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.backBtn:
back();
break;
case R.id.randomBtn:
//this where i want to implement the random data when user click this button
break;
case R.id.copyBtn:
copy();
break;
case R.id.shareBtn:
share();
break;
case R.id.nextBtn:
next();
break;
}
}
private void back() {
if (position > 0) {
position = (position - 1) % quotes_list.size();
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
}
}
private void next() {
position = (position + 1) % quotes_list.size();
quotesTxt.setText(quotes_list.get(position));
countTxt.setText(position + "/" + quotes_list.size());
}
private void copy() {
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("text", quotesTxt.getText());
if (clipboardManager != null) {
clipboardManager.setPrimaryClip(clipData);
}
Toast.makeText(getApplicationContext(), "Copied", Toast.LENGTH_SHORT).show();
}
private void share() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, quotesTxt.getText());
startActivity(Intent.createChooser(intent, "Share to"));
}
}