2016-04-27 97 views
1

我從Sqlite/Android數據庫中獲取重複數據的時間很多。我想在Android遊標中不重複地重新編碼。我每次都需要重複在Sqlite中隨機重複數據android我想要重複隨機數據

請幫忙對不起我的英語

這裏是我的查詢:

public Cursor getQuizQuiestion(String cat, String level, String questionNo) { 
    String QUERY_SELECT_QUIESTION = "SELECT * FROM " +TABLE_QUIESTION +" WHERE "+COL_CAT+ " = '" +cat+"' AND " 
      +COL_LEVEL+ " = " +level+" ORDER BY RANDOM() LIMIT 1"; 

    Cursor cursor = db.rawQuery(QUERY_SELECT_QUIESTION, null); 

    return cursor; 
} 

回答

0

我有簡單的例子,如果我在我的項目中使用我覺得可能是IT的幫助你;

public ArrayList<ModelRandomList> getRandomData(String city){ 

    ArrayList<ModelRandomList> modelRandomListArrayListLists = new ArrayList<ModelRandomList>(); 


    String queryRandomData ="SELECT DISTINCT * FROM "+TABLE_NAME+" WHERE "+COL_CITY + "=?"+ "Order BY RANDOM()"; 

    Cursor cursor = db.rawQuery(queryRandomData,new String[] { String.valueOf(city) }); 

    if(cursor.getCount() != 0){ 

     while (cursor.moveToNext()){ 
      /*mName =cursor.getString(0); 
      mCity = cursor.getString(1); 

      Log.d(TAG,mName +" City "+mCity);*/ 


      ModelRandomList modelRandomList = new ModelRandomList(); 
      modelRandomList.setUserName(cursor.getString(0)); 
      modelRandomList.setUserCity(cursor.getString(1)); 

      modelRandomListArrayListLists.add(modelRandomList); 
     } 

     Random random = new Random(); 

     Collections.shuffle(modelRandomListArrayListLists,random); 
    } 


    Log.d(TAG, "Get Random Totla No of Recode Found "+cursor.getCount()); 



    return modelRandomListArrayListLists; 
} 

,並在你的活動

public class MainActivity extends AppCompatActivity { 
private static final String TAG = MainActivity.class.getSimpleName(); 
private SqlLiteDataBaseHelper sqlLiteDataBaseHelper; 
private Button btGetRandomData; 
private ArrayList<ModelRandomList> modelRandomLists; 
private Button btSingleRandomData; 
private int position = 0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    modelRandomLists = new ArrayList<ModelRandomList>(); 

    sqlLiteDataBaseHelper = new SqlLiteDataBaseHelper(MainActivity.this); 

    try{ 

     if(sqlLiteDataBaseHelper.checkDataBase()){ 

      Log.e(TAG, "Data Base Already Exists"); 

     }else { 

      sqlLiteDataBaseHelper.CopyDataBaseFromAsset(); 
     } 

     sqlLiteDataBaseHelper.openDataBase(); 

     try { 
      Log.e(TAG, "No Of Racode In DataBase " + sqlLiteDataBaseHelper.getDataCount()); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 

    }catch (Exception e){ 
     e.printStackTrace(); 
    } 


    init(); 
} 

private void init() { 

    btGetRandomData = (Button)findViewById(R.id.btRandomData); 
    btSingleRandomData = (Button)findViewById(R.id.btSingleRandomData); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 

    btGetRandomData.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      modelRandomLists = sqlLiteDataBaseHelper.getRandomData("A"); 

      for (ModelRandomList crt : modelRandomLists) { 

       Log.e(TAG, " " + crt.getUserName()); 
       Log.e(TAG, " " + crt.getUserCity()); 

      } 
     } 
    }); 


    btSingleRandomData.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 


      try{ 
       Log.d(TAG,modelRandomLists.get(position).getUserName()); 
       position ++; 

      }catch (Exception e){ 
       e.printStackTrace(); 
       position =0; 
      } 


     } 
    }); 
} 

}

+0

thnx @AndroidGeeks它的工作原理 –

0

沒有辦法,如果你查詢了一遍又一遍,以創造獨特的效果。

由於您並未使用questionNo參數並返回光標:爲什麼不只是刪除限制1並將distinct子句添加到您的語句中?

如果你這樣做的,你可以用你的鼠標來遍歷獨特的問題:

String QUERY_SELECT_QUIESTION = "SELECT DISTINCT * FROM " +TABLE_QUIESTION +" WHERE "+COL_CAT+ " = '" +cat+"' AND " 
     +COL_LEVEL+ " = " +level+" ORDER BY RANDOM()"; 

然後你就可以在問題重複,直到你有沒有或者無論你的條件是:

Cursor cursor = db.rawQuery(QUERY_SELECT_QUIESTION, null); 
try { 
    while (cursor.moveToNext()) { 
     //display question or copy them to a member or whatever 
    } 
} finally { 
    cursor.close(); 
} 
+0

對不起兄弟,但不能understna我沒有良好的知識的Android u能指導我如何從數據庫中隨機數據而不repeting在coursor –

+0

我已經更新了答案。獲取唯一數據的關鍵是隻查詢一次數據,並使用不同的 –

0

你將不得不跟蹤以前見過的問題。這個怎麼樣:

private List<Integer> seenQuestions = new ArrayList<>(); 

public Cursor getUseenQuizQuestion(String cat,String level,String questionNo) { 

    Cursor cursor = getQuizQuiestion(cat, level, questionNo); 
    while(cursor == null) { // This could be an infinite loop!! 
     cursor = getQuizQuiestion(cat, level, questionNo); 
    } 
    return cursor; 
} 

private Cursor getQuizQuiestion(String cat,String level,String questionNo){ 
    String QUERY_SELECT_QUIESTION = "SELECT * FROM " +TABLE_QUIESTION +" WHERE "+COL_CAT+ " = '" +cat+"' AND " 
      +COL_LEVEL+ " = " +level+" ORDER BY RANDOM() LIMIT 1"; 
    Cursor cursor = db.rawQuery(QUERY_SELECT_QUIESTION, null); 
    if(cursor.moveToFirst()) { 
     int resultId = cursor.getString(cursor.getColumnIndexOrThrow("_id")); 
     if(seenQuestions.contains(resultId)) { 
      cursor.close(); 
      return null; 
     } 

     seenQuestions.add(resultId); 
    } 
    return cursor; 
} 

^上面的代碼示例有許多缺陷,可以循環無限。

但重點是你需要跟蹤返回的內容,如果你看到它,再次查詢。

或者,您可以允許您的數據庫查詢返回所有數據,然後使用隨機值來選擇其中一個項目。