aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
authorBananeweizen <bananeweizen@gmx.de>2011-12-18 22:19:38 +0100
committerBananeweizen <bananeweizen@gmx.de>2011-12-18 22:19:38 +0100
commiteac366bad9af1e2ec3b014f953837e2d067c237e (patch)
treea368adef8f7060284a996e422c1b1ff44aae4d0b /main/src
parent439bcd63ead65c48ead621a209b37017a3cf2858 (diff)
downloadcgeo-eac366bad9af1e2ec3b014f953837e2d067c237e.zip
cgeo-eac366bad9af1e2ec3b014f953837e2d067c237e.tar.gz
cgeo-eac366bad9af1e2ec3b014f953837e2d067c237e.tar.bz2
fix #358: have number of caches in list selection
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/StoredList.java20
-rw-r--r--main/src/cgeo/geocaching/cgData.java95
-rw-r--r--main/src/cgeo/geocaching/cgList.java14
-rw-r--r--main/src/cgeo/geocaching/cgeo.java6
-rw-r--r--main/src/cgeo/geocaching/cgeoapplication.java4
-rw-r--r--main/src/cgeo/geocaching/cgeocaches.java30
-rw-r--r--main/src/cgeo/geocaching/cgeogpxes.java2
7 files changed, 98 insertions, 73 deletions
diff --git a/main/src/cgeo/geocaching/StoredList.java b/main/src/cgeo/geocaching/StoredList.java
new file mode 100644
index 0000000..64e2bfd
--- /dev/null
+++ b/main/src/cgeo/geocaching/StoredList.java
@@ -0,0 +1,20 @@
+package cgeo.geocaching;
+
+
+public class StoredList {
+ public static final int STANDARD_LIST_ID = 1;
+
+ public final int id;
+ public final String title;
+ public final int count;
+
+ public StoredList(int id, String title, int count) {
+ this.id = id;
+ this.title = title;
+ this.count = count;
+ }
+
+ public String getTitleAndCount() {
+ return title + " [" + count + "]";
+ }
+}
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java
index 0d48493..7931e24 100644
--- a/main/src/cgeo/geocaching/cgData.java
+++ b/main/src/cgeo/geocaching/cgData.java
@@ -225,6 +225,7 @@ public class cgData {
private boolean initialized = false;
private SQLiteStatement statementDescription;
private SQLiteStatement statementLogCount;
+ private SQLiteStatement statementStandardList;
private static boolean newlyCreatedDatabase = false;
public cgData(Context contextIn) {
@@ -332,6 +333,10 @@ public class cgData {
statementLogCount.close();
statementLogCount = null;
}
+ if (statementStandardList != null) {
+ statementStandardList.close();
+ statementStandardList = null;
+ }
}
private static File backupFile() {
@@ -3099,6 +3104,13 @@ public class cgData {
return statementLogCount;
}
+ private synchronized SQLiteStatement getStatementStandardList() {
+ if (statementStandardList == null) {
+ statementStandardList = databaseRO.compileStatement("SELECT count(_id) FROM " + dbTableCaches + " WHERE reason = " + StoredList.STANDARD_LIST_ID);
+ }
+ return statementStandardList;
+ }
+
public boolean hasLogOffline(final String geocode) {
if (StringUtils.isBlank(geocode)) {
return false;
@@ -3148,60 +3160,67 @@ public class cgData {
}
}
- public List<cgList> getLists(Resources res) {
+ public List<StoredList> getLists(Resources res) {
init();
- List<cgList> lists = new ArrayList<cgList>();
+ List<StoredList> lists = new ArrayList<StoredList>();
+ lists.add(new StoredList(StoredList.STANDARD_LIST_ID, res.getString(R.string.list_inbox), (int) getStatementStandardList().simpleQueryForLong()));
- lists.add(new cgList(cgList.STANDARD_LIST_ID, res.getString(R.string.list_inbox)));
- // lists.add(new cgList(true, 2, res.getString(R.string.list_wpt)));
+ try {
+ String query = "SELECT l._id as _id, l.title as title, COUNT(c._id) as count" +
+ " FROM " + dbTableLists + " l LEFT OUTER JOIN " + dbTableCaches + " c" +
+ " ON l._id + 10 = c.reason" +
+ " GROUP BY l._id" +
+ " ORDER BY l.title COLLATE NOCASE ASC";
- ArrayList<cgList> storedLists = readLists(null, "title COLLATE NOCASE ASC");
- lists.addAll(storedLists);
+ Cursor cursor = databaseRO.rawQuery(query, null);
+ ArrayList<StoredList> storedLists = getListsFromCursor(cursor);
+ lists.addAll(storedLists);
+ } catch (Exception e) {
+ Log.e(Settings.tag, "cgData.readLists: " + e.toString());
+ }
return lists;
}
- private ArrayList<cgList> readLists(String selection, String sorting) {
- ArrayList<cgList> result = new ArrayList<cgList>();
- try {
- Cursor cursor = databaseRO.query(
- dbTableLists,
- new String[] { "_id", "title", "updated", "latitude", "longitude" },
- selection,
- null,
- null,
- null,
- sorting);
-
- if (cursor != null) {
- if (cursor.getCount() > 0) {
- cursor.moveToFirst();
- int indexId = cursor.getColumnIndex("_id");
- int indexTitle = cursor.getColumnIndex("title");
- do {
- cgList list = new cgList(cursor.getInt(indexId) + 10, cursor.getString(indexTitle));
- result.add(list);
- } while (cursor.moveToNext());
- }
-
- cursor.close();
+ private static ArrayList<StoredList> getListsFromCursor(Cursor cursor) {
+ ArrayList<StoredList> result = new ArrayList<StoredList>();
+ if (cursor != null) {
+ if (cursor.getCount() > 0) {
+ cursor.moveToFirst();
+ int indexId = cursor.getColumnIndex("_id");
+ int indexTitle = cursor.getColumnIndex("title");
+ int indexCount = cursor.getColumnIndex("count");
+ do {
+ int count = 0;
+ if (indexCount >= 0) {
+ count = cursor.getInt(indexCount);
+ }
+ StoredList list = new StoredList(cursor.getInt(indexId) + 10, cursor.getString(indexTitle), count);
+ result.add(list);
+ } while (cursor.moveToNext());
}
- } catch (Exception e) {
- Log.e(Settings.tag, "cgData.readLists: " + e.toString());
+
+ cursor.close();
}
return result;
}
- public cgList getList(int id, Resources res) {
- if (id == cgList.STANDARD_LIST_ID) {
- return new cgList(cgList.STANDARD_LIST_ID, res.getString(R.string.list_inbox));
- } else if (id == 2) {
- return new cgList(2, res.getString(R.string.list_wpt));
+ public StoredList getList(int id, Resources res) {
+ if (id == StoredList.STANDARD_LIST_ID) {
+ return new StoredList(StoredList.STANDARD_LIST_ID, res.getString(R.string.list_inbox), (int) getStatementStandardList().simpleQueryForLong());
} else if (id >= 10) {
init();
- ArrayList<cgList> lists = readLists("_id = " + (id - 10), null);
+ Cursor cursor = databaseRO.query(
+ dbTableLists,
+ new String[] { "_id", "title" },
+ "_id = " + (id - 10),
+ null,
+ null,
+ null,
+ null);
+ ArrayList<StoredList> lists = getListsFromCursor(cursor);
if (!lists.isEmpty()) {
return lists.get(0);
}
diff --git a/main/src/cgeo/geocaching/cgList.java b/main/src/cgeo/geocaching/cgList.java
deleted file mode 100644
index 273f6d8..0000000
--- a/main/src/cgeo/geocaching/cgList.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package cgeo.geocaching;
-
-
-public class cgList {
- public static final int STANDARD_LIST_ID = 1;
-
- public int id = 0;
- public String title = null;
-
- public cgList(int idIn, String titleIn) {
- id = idIn;
- title = titleIn;
- }
-}
diff --git a/main/src/cgeo/geocaching/cgeo.java b/main/src/cgeo/geocaching/cgeo.java
index 50d7083..6ebb04e 100644
--- a/main/src/cgeo/geocaching/cgeo.java
+++ b/main/src/cgeo/geocaching/cgeo.java
@@ -327,12 +327,12 @@ public class cgeo extends AbstractActivity {
// context menu for offline button
if (v.getId() == R.id.search_offline) {
- List<cgList> cacheLists = app.getLists();
+ List<StoredList> cacheLists = app.getLists();
int listCount = cacheLists.size();
menu.setHeaderTitle(res.getString(R.string.list_title));
for (int i = 0; i < listCount; i++) {
- cgList list = cacheLists.get(i);
- menu.add(Menu.NONE, MENU_OPEN_LIST + list.id, Menu.NONE, list.title);
+ StoredList list = cacheLists.get(i);
+ menu.add(Menu.NONE, MENU_OPEN_LIST + list.id, Menu.NONE, list.getTitleAndCount());
}
return;
}
diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java
index 5a5042e..ef4d6c8 100644
--- a/main/src/cgeo/geocaching/cgeoapplication.java
+++ b/main/src/cgeo/geocaching/cgeoapplication.java
@@ -667,11 +667,11 @@ public class cgeoapplication extends Application {
storage.clearVisitDate(geocode);
}
- public List<cgList> getLists() {
+ public List<StoredList> getLists() {
return storage.getLists(getResources());
}
- public cgList getList(int id) {
+ public StoredList getList(int id) {
return storage.getList(id, getResources());
}
diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java
index 391a30a..4aa46db 100644
--- a/main/src/cgeo/geocaching/cgeocaches.java
+++ b/main/src/cgeo/geocaching/cgeocaches.java
@@ -155,7 +155,7 @@ public class cgeocaches extends AbstractListActivity {
private ExportFieldNotesThread threadF = null;
private RemoveFromHistoryThread threadH = null;
private int listId = 0;
- private List<cgList> lists = null;
+ private List<StoredList> lists = null;
private GeocodeComparator gcComparator = new GeocodeComparator();
private Handler loadCachesHandler = new Handler() {
@@ -557,10 +557,10 @@ public class cgeocaches extends AbstractListActivity {
case OFFLINE:
listId = Settings.getLastList();
if (listId <= 0) {
- listId = cgList.STANDARD_LIST_ID;
+ listId = StoredList.STANDARD_LIST_ID;
title = res.getString(R.string.stored_caches_button);
} else {
- final cgList list = app.getList(listId);
+ final StoredList list = app.getList(listId);
title = list.title;
}
@@ -720,7 +720,7 @@ public class cgeocaches extends AbstractListActivity {
}
// refresh standard list if it has changed (new caches downloaded)
- if (type == CacheListType.OFFLINE && listId == cgList.STANDARD_LIST_ID && search != null) {
+ if (type == CacheListType.OFFLINE && listId == StoredList.STANDARD_LIST_ID && search != null) {
cgSearch newSearch = cgBase.searchByOffline(coords, cacheType, listId);
if (newSearch != null && newSearch.totalCnt != search.totalCnt) {
refreshCurrentList();
@@ -1152,23 +1152,23 @@ public class cgeocaches extends AbstractListActivity {
}
if (cache.getReason() >= 1) {
menu.add(0, MENU_DROP_CACHE, 0, res.getString(R.string.cache_offline_drop));
- final List<cgList> cacheLists = app.getLists();
+ final List<StoredList> cacheLists = app.getLists();
final int listCount = cacheLists.size();
if (listCount > 1) {
final SubMenu submenu = menu.addSubMenu(0, MENU_MOVE_TO_LIST, 0, res.getString(R.string.cache_menu_move_list));
for (int i = 0; i < listCount; i++) {
- cgList list = cacheLists.get(i);
- submenu.add(Menu.NONE, CONTEXT_MENU_MOVE_TO_LIST + list.id, Menu.NONE, list.title);
+ StoredList list = cacheLists.get(i);
+ submenu.add(Menu.NONE, CONTEXT_MENU_MOVE_TO_LIST + list.id, Menu.NONE, list.getTitleAndCount());
}
}
}
}
private void moveCachesToOtherList() {
- final List<cgList> cacheLists = app.getLists();
+ final List<StoredList> cacheLists = app.getLists();
ArrayList<String> listNames = new ArrayList<String>();
- for (cgList list : cacheLists) {
- listNames.add(list.title);
+ for (StoredList list : cacheLists) {
+ listNames.add(list.getTitleAndCount());
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
@@ -1181,7 +1181,7 @@ public class cgeocaches extends AbstractListActivity {
builder.create().show();
}
- private void moveCachesToList(final cgList list) {
+ private void moveCachesToList(final StoredList list) {
int newListId = list.id;
final boolean moveAll = adapter.getChecked() == 0;
for (final cgCache c : Collections.unmodifiableList(cacheList)) {
@@ -2326,8 +2326,8 @@ public class cgeocaches extends AbstractListActivity {
}
final List<CharSequence> listsTitle = new ArrayList<CharSequence>();
- for (cgList list : lists) {
- listsTitle.add(list.title);
+ for (StoredList list : lists) {
+ listsTitle.add(list.getTitleAndCount());
}
listsTitle.add("<" + res.getString(R.string.list_menu_create) + ">");
@@ -2356,7 +2356,7 @@ public class cgeocaches extends AbstractListActivity {
}
public void switchListById(int id) {
- cgList list = null;
+ StoredList list = null;
if (id >= 0) {
list = app.getList(id);
@@ -2458,7 +2458,7 @@ public class cgeocaches extends AbstractListActivity {
}
private void renameList() {
- final cgList list = app.getList(listId);
+ final StoredList list = app.getList(listId);
handleListNameInput(list.title, R.string.list_dialog_rename_title, R.string.list_dialog_rename, new RunnableWithArgument<String>() {
@Override
diff --git a/main/src/cgeo/geocaching/cgeogpxes.java b/main/src/cgeo/geocaching/cgeogpxes.java
index 8cd9968..cb8167a 100644
--- a/main/src/cgeo/geocaching/cgeogpxes.java
+++ b/main/src/cgeo/geocaching/cgeogpxes.java
@@ -43,7 +43,7 @@ public class cgeogpxes extends FileList<cgGPXListAdapter> {
listId = extras.getInt(EXTRAS_LIST_ID);
}
if (listId <= 0) {
- listId = cgList.STANDARD_LIST_ID;
+ listId = StoredList.STANDARD_LIST_ID;
}
}