How to randomly set text to buttons from SQLite without repetition?

纵然是瞬间 提交于 2019-12-24 03:43:32

问题


I have a db imported to assets, and i read from it and randomly set text to buttons and a texview, with code below:

mDbHelper.open();  

            Cursor c = mDbHelper.getTestData();

            List<Answer> labels = new ArrayList<Answer>();

            labels.add(new Answer(c.getString(2), true));
            labels.add(new Answer(c.getString(3), false));
            labels.add(new Answer(c.getString(4), false));
            labels.add(new Answer(c.getString(5), false));

            Collections.shuffle(labels);

question.setText(c.getString(1));

        bOdgovor1.setText(labels.get(0).option);
        bOdgovor1.setTag(labels.get(0));
        bOdgovor1.setOnClickListener(clickListener);

        bOdgovor2.setText(labels.get(1).option);
        bOdgovor2.setTag(labels.get(1));
        bOdgovor2.setOnClickListener(clickListener);

        bOdgovor3.setText(labels.get(2).option);
        bOdgovor3.setTag(labels.get(2));
        bOdgovor3.setOnClickListener(clickListener);

        bOdgovor4.setText(labels.get(3).option);
        bOdgovor4.setTag(labels.get(3));
        bOdgovor4.setOnClickListener(clickListener);

Here's my TestAdapter code for db:

public Cursor getTestData()
     {;
         try
         {
             String sql ="SELECT * FROM tblPitanja ORDER BY RANDOM() LIMIT 1";

             Cursor mCur = mDb.rawQuery(sql, null);
             if (mCur!=null)
             {
                mCur.moveToNext();
             }
             return mCur;
         }
         catch (SQLException mSQLException) 
         {
             Log.e(TAG, "getTestData >>"+ mSQLException.toString());
             throw mSQLException;
         }
     }

It works perfectly when it comes to setting questions to buttons, but questions repeat. How to avoid that?


回答1:


there are more approaches to solve your problem:

  1. execute the sql-statement (without limiting) at the beginning and move to the next entry of the cursor when a quesion is answered correctly
  2. buffer the questions which where already answered

the second approach could be done as follows:

first, change your method and sql, including a where-clause:

public Cursor getTestData(String whereClause)
 {;
     try
     {
         String sql ="SELECT * FROM tblPitanja WHERE 1 = 1 " + whereClause + " ORDER BY RANDOM() LIMIT 1";
         [...]

second, buffer the already answered questions in your game-class:

add a LinkedList to your game-class

LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();

add already answered questions to the LinkedList:

Cursor c = mDbHelper.getTestData(generateWhereClause());
mAnsweredQuestions.add(c.getLong(0));
List<Answer> labels = new ArrayList<Answer>();
[...]

add a function which generates the where-clause:

private String generateWhereClause(){
    StringBuilder result = new StringBuilder();
    for (Long l : mAnsweredQuestions){
         result.append(" AND " + YOURID + " <> " + l);
    }
    return result.toString();
}



回答2:


You could just save the Cursor objects into an ArrayList and then use contains to find out if that question was alredy asked. (Note: contains useses the equals method)

public class YourClass {
     java.util.ArrayList<Long> cursorList = new java.util.ArrayList<Long>();
     public void YourMethod {

         Cursor c = mDbHelper.getTestData();
         long l = c.getLong(0);


         while(cursorList.contains(l))
         {
              Cursor c = mDbHelper.getTestData();
              l = c.getLong(0);
         }

         cursorList.add(l);

    }
}



回答3:


The solution depends on your application logic. One of possible solutions is to remove "LIMIT 1" from your query and load all questions randomly sorted.

If your app does something like "show me next random question I have never seen before", you have to keep the list of already visited questions (in memory, or in DB - again depends on your application logic. For DB solution is as simple as add visited column to your database.




回答4:


For this problem, Better you maintain the list of question(object or id) your asked. Before display any question to the user check the current question is in your array list or not. If current qus is in arraylist then you can call getTestData(), otherwise you can display the question and add id or qus object to the array list.



来源:https://stackoverflow.com/questions/15185495/how-to-randomly-set-text-to-buttons-from-sqlite-without-repetition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!