SQLite データベースからのクエリによって入力されたListView
がありますが、結果に同じ行が 2 回表示されることがよくあるという問題があります。どこで間違ったのか教えてもらえますか?
とにかく、ここに私のスニペットがあります:
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;
}