Android:SQLite查询在结果上显示两次相同的行

时间:2012-11-22 14:45:38

标签: android sqlite

我有一个ListView,它由来自SQLite数据库的查询填充,但问题是结果通常是两次显示相同的行。谁能告诉我哪里可能出错?

无论如何这里是我的片段:

ListActivity上的

idList = FavoriteProvider.getInstance().getFavoritesByTopicAndMinRating(minRating);
收藏提供商

上的

public List<Long> getFavoritesByTopicAndMinRating(float rating) {
        List<Long> favIdsFromDB = null;

        Log.d(TAG, "loading new current favorites");
        this.currentFavoriteList.clear();

        try {

            favIdsFromDB = Loudmouth.getInstance().getDbHelper()
                    .getIdsFromSelectedTopicsWithMinRatingFav(rating);

            if (favIdsFromDB != null) {
                this.currentFavoriteList.addAll(favIdsFromDB);
            }
            // all loaded again
            setCurrentFavoriteListStateDirty(false);

        } catch (Exception e) {
            Log.e(TAG, "Error loading current favorites");
        }
        return currentFavoriteList;
    }
DatabaseHelper上的

public ArrayList<Long> getIdsFromSelectedTopicsWithMinRatingFav(Float rating) {

        ArrayList<TopicFilterData> topics = getCheckedTopicListFav();

        String topicsAsString = topics.get(0).getTopicName() + "'";
        for (int i = 1; i < topics.size(); i++) {
            topicsAsString = topicsAsString + " OR " + KEY_FAV_CAT + " LIKE '" + topics.get(i) + "'";
        }
        String where = "(" + KEY_FAV_RATING + ">=" + rating + ") AND (" + KEY_FAV_CAT + " LIKE '" + topicsAsString + ")";
        String[] columns = { KEY_FAV_ID };
        Cursor query = db.query(DB_TABLE_FAVORITES, columns, where, null, null, null, KEY_FAV_RATING + " DESC", null);

        ArrayList<Long> resultList = new ArrayList<Long>();
        long id;

        if (query.moveToFirst() == true) {
            for (int i = 0; i < query.getCount(); i++) {
                id = query.getLong(COLUMN_FAV_ID);
                resultList.add(id);
                query.moveToNext();
            }
        }
        query.close();
        return resultList;
    }

private ArrayList<TopicFilterData> getCheckedTopicListFav() {
        Cursor cExisting = db.rawQuery("SELECT * FROM " + DB_TABLE_TOPICSFAV + " ORDER BY " + KEY_TF_NAME + " ASC", null);
        int existingTable = cExisting.getCount();
        cExisting.close();

        Cursor cNew = db.rawQuery("SELECT DISTINCT " + KEY_FAV_CAT + " FROM " + DB_TABLE_FAVORITES + " ORDER BY " + KEY_FAV_CAT + " ASC", null);
        int newTable = cNew.getCount();
        cNew.close();

        if (existingTable != newTable) {
            return getNewTopiclistFav();
        } else {
            return getCheckedTopicListFromDBFav();
        }
    }

private ArrayList<TopicFilterData> getCheckedTopicListFromDBFav() {
        ArrayList<TopicFilterData> resultList = new ArrayList<TopicFilterData>();
        Cursor c = db.rawQuery("SELECT " + KEY_TF_NAME + " FROM " + DB_TABLE_TOPICSFAV + " WHERE " + KEY_TF_SHOW + " = " + 1 + " ORDER BY "
                + KEY_TF_NAME + " ASC", null);

        TopicFilterData tFilterData;
        String topic;

        if (c.moveToFirst() == true) {
            for (int i = 0; i < c.getCount(); i++) {
                topic = c.getString((int) c.getLong(c.getColumnIndexOrThrow(KEY_TF_NAME)));

                // create item and add it to list
                tFilterData = new TopicFilterData(topic, true);

                resultList.add(tFilterData);
                c.moveToNext();
            }
        }
        c.close();
        return resultList;
    }

0 个答案:

没有答案