Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1393
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 30, 20222022-05-30T14:31:08+00:00 2022-05-30T14:31:08+00:00

java – How to get random data from Google Firebase in Android Studio

  • 0

[ad_1]

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:

1:- 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).

2:- 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

https://i.stack.imgur.com/JiTBm.png

Note :- 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 a 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"));
    }

}

[ad_2]

  • 0 0 Answers
  • 11 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4801 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1063 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1010 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.