So I have a SQLite database in my app that stores favorite items (Wallpapers) in it. I'm beginning to learn database management so most of this is new to me. However, I've managed to get the basic database working including adding, removing, and querying...except querying needs a little work.
Basically, the user opens an activity that has an image in it. They open a menu item which adds the current image to the database (using string values since the image is retrieved from the internet).
Next, I've got a special fragment which lists the favorites. When you long press on an item in said list, it'll be removed from the database.
Here's my problem: deleting items works fine except when I delete the 0th item. For some reason, it causes my cursor in getFavorite() to become zero and I get an exception.
See my code below...kind of a lot but I'm hoping someone can help me!
Code
SQLite Database Helper
public class FavoritesHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "favoritesManager";
// Contacts table name
private static final String TABLE_FAVORITES = "favoriteWallpapers";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_URL = "url";
private static final String KEY_FILENAME = "file_name";
private static final String[] COLUMNS = {KEY_ID, KEY_URL, KEY_FILENAME};
public FavoritesHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_FAVORITES + "("
+ KEY_ID + " INTEGER PRIMARY KEY, "
+ KEY_URL + " TEXT, "
+ KEY_FILENAME + " TEXT" + ");";
sqLiteDatabase.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES + ";");
onCreate(sqLiteDatabase);
}
public void addFavorite(Favorite favorite) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_URL, favorite.getURL()); // Favorite URL
values.put(KEY_FILENAME, favorite.getFilename()); // Favorite Filename
// Inserting Row
db.insert(TABLE_FAVORITES, null, values);
db.close(); // Closing database connection
}
public Favorite getFavorite(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_FAVORITES, new String[]{KEY_ID,
KEY_URL, KEY_FILENAME}, KEY_ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
// return contact
return new Favorite(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
}
public ArrayList<Favorite> getAllFavorites() {
ArrayList<Favorite> favoriteList = new ArrayList<>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_FAVORITES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Favorite favorite = new Favorite();
favorite.setID(cursor.getInt(0));
favorite.setURL(cursor.getString(1));
favorite.setFilename(cursor.getString(2));
favoriteList.add(favorite);
} while (cursor.moveToNext());
}
// return contact list
return favoriteList;
}
public int getFavoritesCount() {
SQLiteDatabase db = this.getReadableDatabase();
String countQuery = "SELECT * FROM " + TABLE_FAVORITES + ";";
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
// return count
return count;
}
public void deleteFavorite(Favorite favorite) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_FAVORITES, " id = ?",
new String[]{String.valueOf(favorite.getID())});
db.close();
}
}
Favorite Class
public class Favorite {
int _ID;
String _URL;
String _FILENAME;
public Favorite() {
}
public Favorite(int ID, String URL, String Filename) {
this._ID = ID;
this._URL = URL;
this._FILENAME = Filename;
}
public Favorite(String URL, String Filename) {
this._URL = URL;
this._FILENAME = Filename;
}
public int getID() {
return _ID;
}
public void setID(int _ID) {
this._ID = _ID;
}
public String getURL() {
return _URL;
}
public void setURL(String _URL) {
this._URL = _URL;
}
public String getFilename() {
return _FILENAME;
}
public void setFilename(String _FILENAME) {
this._FILENAME = _FILENAME;
}
}
Custom Image Adapter in Favorites fragment
private class ImageAdapter extends BaseAdapter {
@Override
public int getCount() {
FavoritesHandler favoritesHandler = new FavoritesHandler(getActivity().getBaseContext());
return favoritesHandler.getFavoritesCount();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder gridViewImageHolder;
if (convertView == null) {
view = getActivity().getLayoutInflater().inflate(R.layout.util_grid_item_image, parent, false);
gridViewImageHolder = new ViewHolder();
gridViewImageHolder.imageView = (ImageView) view.findViewById(R.id.util_wallpaper_image);
view.setTag(gridViewImageHolder);
} else {
gridViewImageHolder = (ViewHolder) view.getTag();
}
FavoritesHandler db = new FavoritesHandler(getActivity());
List<Favorite> favorites = db.getAllFavorites();
for (Favorite favorite : favorites) {
favorite = db.getFavorite(position + 1);
imageLoader.displayImage(favorite.getURL() + favorite.getFilename(),
gridViewImageHolder.imageView,
ImageLoaderUtil.setupOptions());
}
return view;
}
}
Exception AFTER deleting first item in displayed list
09-27 02:04:38.641 30338-30338/? E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: FATAL EXCEPTION: main
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: Process: com.hidden.hidden, PID: 30338
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at com.hidden.hidden.FavoritesHandler.getFavorite(FavoritesHandler.java:74)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at com.hidden.hidden.fragments.FavoritesFragment$ImageAdapter.getView(FavoritesFragment.java:136)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2346)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.GridView.onMeasure(GridView.java:1065)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.View.measure(View.java:18788)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.View.measure(View.java:18788)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.View.measure(View.java:18788)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.View.measure(View.java:18788)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:940)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.View.measure(View.java:18788)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.View.measure(View.java:18788)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.View.measure(View.java:18788)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.View.measure(View.java:18788)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.View.measure(View.java:18788)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.View.measure(View.java:18788)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:670)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:606)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
09-27 02:04:40.195 30338-30338/com.hidden.hidden E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I've been working on trying to find a solution for this for about 3 days now...I think it needs a set of new eyes.
Aucun commentaire:
Enregistrer un commentaire