1

我目前正在开发一个应用程序。它的作用是,用户会输入一些句子,然后应用程序会得到模棱两可的词,然后显示它的含义。在我的表中,我有 _id、word、meaning、definition_number 等字段。

样本数据:
_id 词义定义_编号
1 中断从某事暂停 1
2 中断切割成碎片 2

如果用户输入:我的休息时间非常快。预期的输出将是:
模棱两可的词:中断
含义:暂停某事

我想随机显示它。这是我的DBHelper.class中的一段代码:

        public Cursor getAllWords()
{
    Cursor localCursor =   
            //this.myDataBase.query(DB_TABLE, new String[] { 
            //      KEY_ID, KEY_WORD, KEY_MEANING }, null, null, null, null, null);//"RANDOM()");
        //this.myDataBase.query(DB_TABLE, new String[] { 
        //      KEY_ID, KEY_WORD, KEY_MEANING }, null, null, null, null, "RANDOM()", " 1");
            this.myDataBase.query(DB_TABLE, new String[] { 
                            KEY_ID, KEY_WORD, KEY_MEANING }, 
                            null, null, null, null, "RANDOM()");
    if (localCursor != null){
      localCursor.moveToFirst(); 
    }

    return localCursor; 


}

MainActivity.class

    ArrayList<String> colWords = new ArrayList<String>();
ArrayList<String> colMeanings = new ArrayList<String>();
String[] words;
String[] meanings;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initControls();

}

private void initControls() {
    // TODO Auto-generated method stub
    text = (EditText) findViewById (R.id.editText1);

    view = (TextView) findViewById (R.id.textView1);

    clear = (Button) findViewById (R.id.button2);

    clear.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            text.setText("");
            view.setText("");

        }
    });


    connectDB();

    ok = (Button) findViewById (R.id.button1);
    ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Log.d(strWord, strWord);

            strWord = text.getText().toString();
            if(strWord.isEmpty()){
                Toast.makeText(MainActivity.this, "Please input some data", Toast.LENGTH_SHORT).show();
            } else {
            checkAmbiguousWord();
            }
        }   
    });
}

private void connectDB(){
    dbHelper = new DBHelper(MainActivity.this);

    try {

            dbHelper.createDataBase();

    } catch (IOException ioe) {

            throw new Error("Unable to create database");

    }

    try {

            dbHelper.openDataBase();

    } catch (SQLException sqle) {

            throw sqle;

    }

    cursor = dbHelper.getAllWords();




    /*strWord = cursor.getString(cursor.getColumnIndex(DBHelper.KEY_WORD))
            + cursor.getString(cursor.getColumnIndex(DBHelper.KEY_MEANING)); */

    colWords.clear();///added code
    colMeanings.clear();///added code
    /*
    for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) {
        colWords.add(cursor.getString(1));
        colMeanings.add(cursor.getString(2));
        String records = cursor.getString(0);

        Log.d("Records", records);
    } */

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                colWords.add(cursor.getString(1)); 
                colMeanings.add(cursor.getString(2));   
                String records = cursor.getString(0);

                Log.d("Records", records);
            } while (cursor.moveToNext());
        }
    }

}

private void checkAmbiguousWord(){
    final String textToCheck = text.getText().toString();
    List<Integer> ambiguousIndexes = findAmbiguousWordIndexes(textToCheck);
    view.setText(!ambiguousIndexes.isEmpty() ? 
            ambigousIndexesToMessage(ambiguousIndexes) : "No ambiguous word/s found.");
}

/**
 * @param text checked for ambiguous words
 * @return the list of indexes of the ambiguous words in the {@code words} array          
 */
private List<Integer> findAmbiguousWordIndexes(String text) {
    final String lowerCasedText = text.toLowerCase();
    final List<Integer> ambiguousWordIndexList = new ArrayList<Integer>();

    words = (String[]) colWords.toArray(new String[colWords.size()]);
    meanings = (String[]) colMeanings.toArray(new String[colMeanings.size()]);

    for (int i = 0; i < words.length; i++) {
        if (lowerCasedText.contains(words[i].toLowerCase())) {
            ambiguousWordIndexList.add(i);
        }
    }
    return ambiguousWordIndexList;
} 

public String ambigousIndexesToMessage(List<Integer> ambiguousIndexes) {
    // create the text using the indexes
    // this is an example implementation
    StringBuilder sb = new StringBuilder();

    for (Integer index : ambiguousIndexes) {
        sb.append("Ambiguous words: ");
        sb.append(words[index] + "\nMeaning: " + meanings[index] + "\n");
        sb.append("");
    }
    return sb.toString(); 
}

但它所做的只是显示两条记录。id 1 和 id 2。我只想随机显示一条记录。我真的需要这方面的帮助。有任何想法吗?我很乐意欣赏它。

4

1 回答 1

3

您有RANDOM()订单,但您还需要添加 aLIMIT 1以仅返回一个结果行。有一个重载SQLiteDatabase.query()需要一个限制参数

this.myDataBase.query(DB_TABLE, new String[] { 
                        KEY_ID, KEY_WORD, KEY_MEANING }, 
                        null, null, null, null, "RANDOM()", "1");
于 2013-10-22T07:36:17.393 回答