2013-12-08 112 views
0

在這裏搜索和使用所有資源,但似乎沒有任何工作與我的項目。Android sqlite:從數據庫檢索隨機數據

我需要在抖動設備時檢索隨機數據。

我已經有我的搖動檢測。 我的問題是我不能檢索一個隨機數據。

在設備上測試。應用程序崩潰。

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"; 
} 

已經閱讀:

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

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

但對我來說

不起作用
+0

「不起作用」是什麼意思? – laalto

回答

0
  1. 您正在尋找的功能RANDOM()RANDON()

  2. query()取其ORDER BYLIMIT PARAMS單獨指定參數時,沒有ORDER BYLIMIT關鍵字。

+0

對不起,只是一個錯字錯誤將其更改爲RANDOM()它會使應用程序崩潰。 –

+0

@ FanBoy-designAnd-concept對於崩潰,在問題中總是包含來自logcat的異常stacktrace。這是滿足「什麼」不起作用「意味着」問題的一種方式。 – laalto

+0

得到了錯誤,我認爲它是我的查詢。 的NullPointerException - 光標光標= database.query(MySQLiteHelper.TABLE_COMMENTS, \t \t \t \t allColumns,NULL,NULL,NULL,NULL,NULL); 當我登錄這個查詢其空。 –