0

我有一个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;
    }
4

0 回答 0