From c2ef86ea544035ab9f0fab17d8fb6038871f8c84 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Fri, 10 Feb 2012 00:42:54 -0500 Subject: [PATCH] Fixed java.lang.IllegalStateException thrown by RollHistortDatabaseAdapter RollHistortDatabaseAdapter only updates the roll count in notifyDataSetChanged() re-added roll thread added boolean isRolling to prevent multiple rolls at the same time Change-Id: I47fea7af989d0b2239ad8c5082649022836a2263 Signed-off-by: Ricky Barrette --- .../android/ExaltedDice/ExaltedDice.java | 50 +++++++++++-------- .../RollHistoryDatabaseAdapter.java | 21 ++++++-- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/ExaltedDice/src/com/TwentyCode/android/ExaltedDice/ExaltedDice.java b/ExaltedDice/src/com/TwentyCode/android/ExaltedDice/ExaltedDice.java index 8e1ad81..966e376 100755 --- a/ExaltedDice/src/com/TwentyCode/android/ExaltedDice/ExaltedDice.java +++ b/ExaltedDice/src/com/TwentyCode/android/ExaltedDice/ExaltedDice.java @@ -53,6 +53,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic private NumberPicker mModPicker; private ProgressBar mRollProgress; private View mRollButton; + private boolean isRolling = false; /** * Applies the presets from the provided roll @@ -382,28 +383,32 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic * @author ricky barrette */ public void rollDice() { - mListAdapter.notifyDataSetInvalidated(); - mRollButton.setEnabled(false); - mRollProgress.setVisibility(View.VISIBLE); - -// new Thread( new Runnable() { -// @Override -// public void run(){ - // vibrate for 50 milliseconds - vibrate(50); - - int rollId = mDb.getGameRollCount(mGameId) +1; - - ContentValues roll = new ContentValues(); - roll.put(Database.KEY_D_TYPE, mDiceValues[mDPicker.getValue()]); - roll.put(Database.KEY_NUMBER, mNumberPicker.getValue()); - roll.putAll(results(mNumberPicker.getValue())); - - roll.put(Database.KEY_MOD, DatabaseUtils.sqlEscapeString(mModValues[mModPicker.getValue()])); - - mDb.updateGame(mGameId, mGameName, roll, rollId); -// } -// }).start(); + if(!isRolling){ + isRolling = true; + + mListAdapter.notifyDataSetInvalidated(); + mRollButton.setEnabled(false); + mRollProgress.setVisibility(View.VISIBLE); + + new Thread( new Runnable() { + @Override + public void run(){ + // vibrate for 50 milliseconds + vibrate(50); + + int rollId = mDb.getGameRollCount(mGameId) +1; + + ContentValues roll = new ContentValues(); + roll.put(Database.KEY_D_TYPE, mDiceValues[mDPicker.getValue()]); + roll.put(Database.KEY_NUMBER, mNumberPicker.getValue()); + roll.putAll(results(mNumberPicker.getValue())); + + roll.put(Database.KEY_MOD, DatabaseUtils.sqlEscapeString(mModValues[mModPicker.getValue()])); + + mDb.updateGame(mGameId, mGameName, roll, rollId); + } + }).start(); + } } /** @@ -494,6 +499,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic @Override public void onDatabaseInsertComplete() { + isRolling = false; this.runOnUiThread(new Runnable(){ @Override public void run(){ diff --git a/ExaltedDice/src/com/TwentyCode/android/ExaltedDice/RollHistoryDatabaseAdapter.java b/ExaltedDice/src/com/TwentyCode/android/ExaltedDice/RollHistoryDatabaseAdapter.java index 0406c9e..23e845b 100644 --- a/ExaltedDice/src/com/TwentyCode/android/ExaltedDice/RollHistoryDatabaseAdapter.java +++ b/ExaltedDice/src/com/TwentyCode/android/ExaltedDice/RollHistoryDatabaseAdapter.java @@ -8,7 +8,6 @@ package com.TwentyCode.android.ExaltedDice; import android.content.ContentValues; import android.content.Context; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -21,11 +20,11 @@ import android.widget.TextView; */ public class RollHistoryDatabaseAdapter extends BaseAdapter { - private static final String TAG = "RollHistoryDatabaseAdapter"; private long mGameId; private Database mDb; private String mGameName; private LayoutInflater mInflater; + private int mCount; /** * Creates a new RollHistoryDatabaseAdapter @@ -36,6 +35,7 @@ public class RollHistoryDatabaseAdapter extends BaseAdapter { mGameName = db.getGameName(gameId); mDb = db; mInflater = LayoutInflater.from(context); + mCount = mDb.getGameRollCount(mGameId); } /** @@ -44,8 +44,7 @@ public class RollHistoryDatabaseAdapter extends BaseAdapter { */ @Override public int getCount() { - Log.v(TAG, "getCount() "+mDb.getGameRollCount(mGameId)); - return mDb.getGameRollCount(mGameId); + return mCount; } /** @@ -95,6 +94,20 @@ public class RollHistoryDatabaseAdapter extends BaseAdapter { return convertView; } + /** + * (non-Javadoc) + * @see android.widget.BaseAdapter#notifyDataSetChanged() + */ + @Override + public void notifyDataSetChanged() { + mCount = mDb.getGameRollCount(mGameId); + super.notifyDataSetChanged(); + } + + /** + * A simple holder class + * @author ricky barrette + */ private class ViewHolder { TextView mRoll; TextView mStats;