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 a 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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8826361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:07:00+00:00 2026-06-14T07:07:00+00:00

I know that in Android the SQL Order BY RANDOM() LIMIT 2 will output

  • 0

I know that in Android the SQL Order BY RANDOM() LIMIT 2 will output two random strings, but it puts both strings into the same TextView. I’d like to get two random strings and put them in two separate TextViews. This code gets two random strings and outputs them into the same textview:

    //--- DBAdapter method
public String getRandomHP1() {
    Cursor cursor = this.db.query(
            "hdtable Order BY RANDOM() LIMIT 2",
            new String[] { KEY_HP }, null, null, null, null, null);
    String result = "";

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        result = result + cursor.getString(0);
    }
    return result;
}

//--- Main Activity
String r1 = dba.getRandomHP1();
dba.close();
optionB_TV.setText(r1);

So then I tried the below code to try and separate the two random strings into two string arrays, but it crashed the app. Can someone let me know what I’m doing wrong? Thanks!

    //--- DBAdapter method
public String[] getRandomHP2() {
    Cursor cursor = this.db.query(
            "hdtable Order BY RANDOM() LIMIT 2",
            new String[] { KEY_HP }, null, null, null, null, null);

    if (cursor != null) {
        for (cursor.moveToFirst(); !cursor.isAfterLast();){
            String colStrings[] = new String[2];
            colStrings[0] = cursor.getString(0);
            colStrings[1] = cursor.getString(1);
            return colStrings;
        }
    }
    return null;
}

//---Main Activity
String[] r2 = dba.getRandomHP2();
dba.close();
optionB_TV.setText(r2[0]);
optionC_TV.setText(r2[1]);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:07:01+00:00Added an answer on June 14, 2026 at 7:07 am

    Using the code

        for (cursor.moveToFirst(); !cursor.isAfterLast();){
           String colStrings[] = new String[2];
            colStrings[0] = cursor.getString(0);
            colStrings[1] = cursor.getString(1);
            return colStrings;
        }
    

    You are constantly moving to the first value in your cursor.. You will never get the 2nd value.. Additionally, you are trying to retrieve a column that doesn’t exist using cursor.getString(1) which is likely where your crash is occuring

    try this instead

        int i = 0;
        String colStrings[] = new String[cursor.getCount()];
        while (cursor.moveToNext())
        {
         colStrings[i] = cursor.getString(0);
         i++;
        }
    
        return colStrings;
    

    Also.. don’t forget to close your cursor when you are done with it..

    Just call cursor.close(); to do so

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that Android has a JSON parser baked in but I was wondering
So I'm making an android app that has more than 100 buttons,but you know
I've started building an android app that will connect to a sql server and
I know that android will pick the resources from the corresponding folders in the
I know that android doesn't highlight anything in TouchMode. But I am doing something
I know that Android has some ad networks to monetize the apps. But I'm
I know that a style can be inherited in android via the parent property
I want to know that how voice recognition in android works? Which library it
I wanted to know that how can we find how much time android device
I want to know that how to follow a page on Twitter using android

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.