You need to enable JavaScript to run this app. Android SQLite如何在Spinner选中时展示指定难度的高分数据
优惠活动
大模型
产品
解决方案
定价
更多

Android SQLite如何在Spinner选中时展示指定难度的高分数据

功能实现方案

1. 改造数据库查询方法

新增支持按难度筛选的查询接口,原有全量查询方法可以保留:

// 新增按难度筛选的查询方法,difficult参数为传入的选中难度值
public Data_Collect[] getScoreByDifficulty(String difficult) {
    Data_Collect[] data = null;
    String[] colums = {"Difficult", "BRedo", "BCorrect"};
    String orderBy = "BCorrect DESC"; // 高分记录建议按正确率倒序,可自行调整
    // 筛选条件:Difficult = ?
    String selection = "Difficult = ?";
    String[] selectionArgs = new String[]{difficult};
    
    Cursor cursor = getReadableDatabase().query(
            "score",
            colums,
            selection, 
            selectionArgs, 
            null, null,
            orderBy);
    if (cursor.getCount()>0 && cursor.moveToFirst()) {
        data = new Data_Collect[cursor.getCount()];
        int counter = 0;
        do {
            data[counter] = new Data_Collect(
                    cursor.getString(0),
                    cursor.getInt(1),
                    cursor.getInt(2));
            counter++;
        } while (cursor.moveToNext());
    }
    cursor.close();
    return data;
}

注意:如果需要支持展示全部难度的记录,可以保留原有getScore()方法做兼容。


2. Activity侧封装列表刷新逻辑

把查询数据+刷新ListView的逻辑抽成公共方法,避免重复代码:

// 成员变量,保存难度选项数组,确保和数据库存储的Difficult字段值完全一致
private String[] Difficulty = {"简单", "中等", "困难"};
private RecordAdapter adapter;

// 新增刷新列表的公共方法,传入null时查询全量数据
private void refreshScoreList(String selectDifficult) {
    new Thread(() -> {
        RecordDB db = new RecordDB(Score.this);
        Data_Collect[] data;
        if (selectDifficult == null) {
            data = db.getScore();
        } else {
            data = db.getScoreByDifficulty(selectDifficult);
        }
        db.close();
        runOnUiThread(() -> {
            if (data != null) {
                if (adapter == null) {
                    // 首次加载初始化Adapter
                    adapter = new RecordAdapter(Score.this, R.layout.recordlist, R.id.text_Difficulty, data);
                    ScoreView.setAdapter(adapter);
                } else {
                    // 后续选中刷新直接更新Adapter数据
                    adapter.clear();
                    adapter.addAll(data);
                    adapter.notifyDataSetChanged();
                }
            } else {
                // 无数据时清空列表,可自行添加空布局提示
                if (adapter != null) {
                    adapter.clear();
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }).start();
}

3. 初始化和Spinner回调逻辑修改

3.1 初始加载逻辑调整

ArrayAdapter<String> adapterDifficult = new ArrayAdapter<>(
        Score.this, android.R.layout.simple_list_item_1, Difficulty
);
SpinnerDifficulty.setAdapter(adapterDifficult);
SpinnerDifficulty.setOnItemSelectedListener(this);
// 首次进入默认加载第一个难度的记录,或者传null加载全部
refreshScoreList(Difficulty[0]);

3.2 实现Spinner选中回调

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // 拿到选中的难度值,调用刷新方法
    String selectDifficult = Difficulty[position];
    refreshScoreList(selectDifficult);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
    // 无选中时默认加载全部,可自行调整逻辑
    refreshScoreList(null);
}

注意事项

  • 确保Difficulty数组的取值和数据库score表中Difficult字段存储的内容完全一致,否则会出现查询无结果的问题
  • 如果需要添加“全部”选项,只需要在Difficulty数组第一个位置加“全部”,在refreshScoreList方法中判断如果选中值为“全部”就调用原有全量查询方法即可

内容的提问来源于stack exchange,提问作者Calvin Lim

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 12:21:02