Skip to content

Commit 2dd2439

Browse files
committed
Fix CursorIndexOutOfBoundsException
1 parent acda5e8 commit 2dd2439

3 files changed

Lines changed: 22 additions & 27 deletions

File tree

app/src/main/java/com/tomatodev/timerdroid/fragments/CategoriesFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void fillData() {
5353
setListAdapter(items);
5454

5555
ListView lv = getListView();
56-
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
56+
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
5757

5858
@Override
5959
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

app/src/main/java/com/tomatodev/timerdroid/fragments/TimerFragment.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -269,26 +269,27 @@ private void adjustCategorySpinner(int catID) {
269269

270270
// loop over all categories to find the position for the current
271271
// category id
272-
cursorAllCat.moveToFirst();
273-
int position = 1;
274-
do {
275-
if (cursorAllCat.getInt(0) == catID)
276-
break;
277-
278-
cursorAllCat.moveToNext();
279-
position++;
280-
} while (!cursorAllCat.isLast());
281-
282-
String[] columns = new String[] { CategoriesTable.CATEGORIES_KEY_NAME };
283-
int[] to = new int[] { android.R.id.text1 };
284-
285-
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this.getActivity(),
286-
android.R.layout.simple_spinner_item, cursorAllCat, columns, to);
287-
288-
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
289-
categorySpinner.setAdapter(mAdapter);
290-
291-
categorySpinner.setSelection(position - 1);
272+
if (cursorAllCat.getCount() > 0) {
273+
cursorAllCat.moveToFirst();
274+
int position = 1;
275+
do {
276+
if (cursorAllCat.getInt(0) == catID)
277+
break;
278+
279+
cursorAllCat.moveToNext();
280+
position++;
281+
} while (!cursorAllCat.isLast());
282+
283+
String[] columns = new String[] { CategoriesTable.CATEGORIES_KEY_NAME };
284+
int[] to = new int[] { android.R.id.text1 };
285+
286+
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this.getActivity(),
287+
android.R.layout.simple_spinner_item, cursorAllCat, columns, to);
288+
289+
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
290+
categorySpinner.setAdapter(mAdapter);
291+
categorySpinner.setSelection(position - 1);
292+
}
292293
}
293294

294295
private Cursor getCategoriesCursor() {

app/src/main/res/layout/categories_list.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)