You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安卓SQL数据库取10个不重复随机词显示到TextView的技术咨询

安卓SQLite随机选取10个不重复单词并展示的实现方案

嘿,刚好我之前处理过类似的需求,给你一套适配单词场景的完整实现方案,一步步来搞定:

第一步:从SQLite数据库获取所有单词列表

首先你需要从你的SQLite数据库里把20个单词全部查出来,返回一个字符串列表。在你的SQLiteOpenHelper子类里添加这个查询方法就行:

// 在你的SQLiteOpenHelper子类中实现获取所有单词的方法
public List<String> getAllWords() {
    List<String> wordList = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    // 替换成你实际的表名和单词列名
    Cursor cursor = db.rawQuery("SELECT word FROM vocabulary_table", null);

    if (cursor.moveToFirst()) {
        do {
            // 确保这里的列名和你数据库里的一致
            String word = cursor.getString(cursor.getColumnIndexOrThrow("word"));
            wordList.add(word);
        } while (cursor.moveToNext());
    }
    // 别忘了关闭游标和数据库,避免内存泄漏
    cursor.close();
    db.close();
    return wordList;
}

第二步:生成10个不重复的随机单词

这里有两种常用的方式,选哪种看你的需求:

方式1:用HashSet生成不重复索引(不打乱原列表)

这种方式适合你不想改变原单词列表顺序的场景,通过随机索引来选取:

// 在Activity的onCreate方法中获取单词列表
List<String> allWords = yourDbHelperInstance.getAllWords();
List<String> randomWordList = new ArrayList<>();
Random random = new Random();

// 先判断数据库里的单词数量是否足够10个,避免数组越界
if (allWords.size() >= 10) {
    Set<Integer> selectedIndices = new HashSet<>();
    // 生成10个不重复的随机索引
    while (selectedIndices.size() < 10) {
        int randomIndex = random.nextInt(allWords.size());
        selectedIndices.add(randomIndex);
    }
    // 根据索引取出对应的单词
    for (int index : selectedIndices) {
        randomWordList.add(allWords.get(index));
    }
} else {
    // 如果单词不足10个,直接取全部(也可以加个提示给用户)
    randomWordList.addAll(allWords);
}

方式2:打乱列表后取前10个(更简洁)

如果不需要保留原列表的顺序,这种方式代码更简洁:

List<String> allWords = yourDbHelperInstance.getAllWords();
List<String> shuffledWords = new ArrayList<>(allWords);
// 打乱列表顺序
Collections.shuffle(shuffledWords);

// 取前10个,不足的话取全部
List<String> randomWordList = shuffledWords.size() >= 10 
    ? shuffledWords.subList(0, 10) 
    : shuffledWords;

第三步:将随机单词绑定到10个TextView上

把布局里的10个TextView放到数组里,循环赋值就行,这样比一个个setText更高效:

// 替换成你布局里对应的TextView ID
TextView[] wordTextViews = {
    findViewById(R.id.tv_word1),
    findViewById(R.id.tv_word2),
    findViewById(R.id.tv_word3),
    findViewById(R.id.tv_word4),
    findViewById(R.id.tv_word5),
    findViewById(R.id.tv_word6),
    findViewById(R.id.tv_word7),
    findViewById(R.id.tv_word8),
    findViewById(R.id.tv_word9),
    findViewById(R.id.tv_word10)
};

// 给每个TextView设置对应的单词
for (int i = 0; i < randomWordList.size(); i++) {
    wordTextViews[i].setText(randomWordList.get(i));
}

// 如果单词不足10个,把剩余的TextView置空或显示提示
for (int i = randomWordList.size(); i < wordTextViews.length; i++) {
    wordTextViews[i].setText("暂无单词");
}

重要注意事项

  • 避免ANR:数据库查询属于耗时操作,最好放在子线程里执行!如果是Java项目可以用AsyncTask,Kotlin项目推荐用协程(lifecycleScope),示例如下:
    // Java用AsyncTask的示例
    private class FetchRandomWordsTask extends AsyncTask<Void, Void, List<String>> {
        @Override
        protected List<String> doInBackground(Void... voids) {
            List<String> allWords = yourDbHelperInstance.getAllWords();
            List<String> shuffledWords = new ArrayList<>(allWords);
            Collections.shuffle(shuffledWords);
            return shuffledWords.size() >=10 ? shuffledWords.subList(0,10) : shuffledWords;
        }
    
        @Override
        protected void onPostExecute(List<String> randomWords) {
            super.onPostExecute(randomWords);
            // 在这里执行TextView绑定操作(onPostExecute在主线程)
        }
    }
    
    // 在onCreate中启动任务
    new FetchRandomWordsTask().execute();
    
  • 替换占位符:代码里的表名、列名、TextView ID都要替换成你项目里的实际内容!

内容的提问来源于stack exchange,提问作者El Hafed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:29:44