0

Been searching and using all the resources here but nothing seems to work with my project.

i need to retrieve a random data upon shaking the device.

i already have my shake detection. my problem is i cant retrieve a random data.

tested on a device. the application crashes.

mainactivity.java

DataSource DS = new DataSource(this);

public void onShake(float force) {

    // Called when Motion Detected
    Toast.makeText(getBaseContext(), "Motion detected", 
            Toast.LENGTH_SHORT).show();

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // Do something after 5s = 5000ms
            TextView text = (TextView) findViewById(R.id.textView1);
            text.setText(DS.getRandomQuote());

        }
    }, 2000);


public String getRandomQuote() {

    Cursor c = database.query(MysqLiteHelper.TABLE_COMMENTS,
        new String[] { MysqLiteHelper.COLUMN_COMMENT}, null, null, null, null, "ORDER BY RANDON() LIMIT 1");

    if(c.moveToFirst())
        return c.getString(c.getColumnIndex(MysqLiteHelper.COLUMN_COMMENT));
    else 
       return "nothing";
}

Already Read:

How can select random data from database and display in textview?

and

How can select random data from database and display in textview?

but doesn't work for me

1
  • What does "doesn't work" mean? Commented Dec 8, 2013 at 19:36

1 Answer 1

0
  1. The function you're looking for is RANDOM() not RANDON()

  2. query() takes its ORDER BY and LIMIT params in separate args, without the ORDER BY and LIMIT keywords.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry just a typo error change it to RANDOM() it crashes the application.
@FanBoy-designAnd-concept For crashes, always include the exception stacktrace from logcat in the question. That's one way to satisfy that "What does \"doesn't work\" mean" question.
got the error i think its my Query. NullPointerException - Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS, allColumns, null, null, null, null, null); when i log this query its null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.