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

SQLite单列数据展示至ListView异常问题求助

解决ListView重复显示、筛选条件失效的问题

让我们一步步排查并修复你的问题:

1. 核心问题:数据存储错误导致筛选失效

在addUser方法中,你错误地将universities.getGre()的值存入了score字段:

values.put(COLUMN_SCORE, universities.getGre());

这意味着数据库中存储的分数并不是你期望的大学录取分数,而是其他值(比如GRE分数),所以当你用score >= input筛选时,结果自然不符合预期。你应该改为调用实体类中获取分数的方法,比如getScore():

values.put(COLUMN_SCORE, universities.getScore());

2. 冗余代码导致的游标异常

在getData方法里,你写了一个完全没用的空循环:

while (cursor.moveToNext()){ }

这会让游标在遍历完数据后继续移动到末尾,虽然不会直接导致重复,但属于无效代码,应该删除。另外,Arrays.toString(new List[]{attrStr});这行代码也没有任何作用,同样可以删掉。

3. 处理重复的大学名称

如果你的数据库中存在重复的univName记录,查询结果会包含重复项。可以在SQL查询语句中添加DISTINCT关键字来去重:

select DISTINCT univName from universityFinder where score >= ?

4. 规范查询与资源释放

  • 使用参数化查询(?占位符)代替字符串拼接,避免潜在的SQL注入风险,同时让代码更规范。
  • 游标使用完毕后必须关闭,防止数据库资源泄漏。

修正后的数据库操作类代码

public class UniversityFinderDB extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    static final String DATABASE_NAME = "universities.db";
    private static final String TABLE_NAME = "universityFinder";
    private static final String COLUMN_UNIVID = "univId";
    private static final String COLUMN_UNIVERSITYNAME = "univName";
    private static final String COLUMN_SCORE = "score";

    public UniversityFinderDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        String TABLE_CREATE = "create table IF NOT EXISTS universityFinder (univId integer PRIMARY KEY , univName varchar not null, score integer not null);";
        database.execSQL(TABLE_CREATE);
    }

    public void addUser(Universities universities) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_UNIVID, universities.getUnivId());
        values.put(COLUMN_UNIVERSITYNAME, universities.getUnivName());
        // 修复:存入正确的score字段值
        values.put(COLUMN_SCORE, universities.getScore());
        db.insert(TABLE_NAME, null, values);
        db.close();
    }

    public List<String> getData(int input) {
        // 修复:添加DISTINCT去重,使用参数化查询
        String query = "select DISTINCT univName from universityFinder where score >= ?";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(query, new String[]{String.valueOf(input)});
        List<String> attrStr = new Vector<>();

        if (cursor.moveToFirst()) {
            do {
                attrStr.add(cursor.getString(cursor.getColumnIndex(COLUMN_UNIVERSITYNAME)));
            } while (cursor.moveToNext());
        }
        // 修复:关闭游标,释放资源
        cursor.close();
        db.close();
        return attrStr;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(query);
        onCreate(db);
    }
}

额外建议

  • 如果你不确定数据库中的数据是否正确,可以使用SQLite查看工具(比如DB Browser for SQLite)打开universities.db文件,检查score字段的值是否符合预期。
  • 实体类Universities请确保包含getScore()方法,并且该方法返回的是正确的大学录取分数。

内容的提问来源于stack exchange,提问作者Prathmesh Pardhiye

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:41:22