mercredi 7 janvier 2015

[Q] IllegalArgumentException when calling method in database topic

00:33






Hello everybody. I've tried to make a suitable database for my game that will represent shop stock. The problem is when I call the purchase() method, I got that exception mentioned in the title. The problem is that I need to chceck depending on ID if I need to increment number of purchases or i just unlock the whole item. Here is my code for the database


Code:


public class MonsterTapDatabase extends SQLiteOpenHelper {
    static final String dbName = "MonsterTapDb";
    static final int version = 1;
    static final String tTableName = "Shop";
    static final String fItemID = "ItemID";
    static final String fItemName = "ItemName";
    static final String fItemNumberOfPurchases = "NumberOfPurchases";
    static final String fItemIsLocked = "IsItemLocked";


    public MonsterTapDatabase(Context context) {
        super(context, dbName, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS "+tTableName+" (" +
                fItemID + " INTEGER PRIMARY KEY , " +
                fItemName + " TEXT , " +
                fItemNumberOfPurchases + " INT, " +
                fItemIsLocked + " TEXT" +
                ")");
        ContentValues cv = new ContentValues();
        cv.put(fItemID, 1);
        cv.put(fItemName, "Lives");
        cv.put(fItemNumberOfPurchases, 0);
        cv.put(fItemIsLocked, "true");
        sqLiteDatabase.insert(tTableName, null, cv);
        cv.put(fItemID, 2);
        cv.put(fItemName, "Hardmode");
        cv.put(fItemNumberOfPurchases, 0);
        cv.put(fItemIsLocked, "true");
        sqLiteDatabase.insert(tTableName, null, cv);
        cv.put(fItemID, 3);
        cv.put(fItemName, "Reversed");
        cv.put(fItemNumberOfPurchases, 0);
        cv.put(fItemIsLocked, "true");
        sqLiteDatabase.insert(tTableName, null, cv);
        cv.put(fItemID, 4);
        cv.put(fItemName, "Reversed Hardmode");
        cv.put(fItemNumberOfPurchases, 0);
        cv.put(fItemIsLocked, "true");
        sqLiteDatabase.insert(tTableName, null, cv);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + tTableName);
        onCreate(sqLiteDatabase);
    }
    public void purchase(int ID){
        if(ID==1){
            SQLiteDatabase myDB = this.getReadableDatabase();
            String[] mySearch = new String[]{String.valueOf(ID)};
            Cursor myCursor = myDB.rawQuery("SELECT "+ fItemNumberOfPurchases +" FROM "+ tTableName +" WHERE "+ fItemID +"=?",mySearch);
            myCursor.moveToFirst();
            int index = myCursor.getColumnIndex(fItemNumberOfPurchases);
            if(index<5){
                SQLiteDatabase db = this.getWritableDatabase();
                ContentValues cv = new ContentValues();
                cv.put(fItemNumberOfPurchases, index);
                db.update(tTableName,cv, fItemID, new String[]{String.valueOf(ID)});
            }
            else{
                SQLiteDatabase db = this.getWritableDatabase();
                ContentValues cv = new ContentValues();
                cv.put(fItemIsLocked, false);
                db.update(tTableName,cv, fItemID, new String[]{String.valueOf(ID)});
            }
            myCursor.close();
        }
        else{
            SQLiteDatabase myDB = this.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(fItemIsLocked, false);
            cv.put(fItemNumberOfPurchases, 1);
            myDB.update(tTableName, cv, fItemID+"=?", new String []{String.valueOf(ID)});
        }
    }
    public String isPurchased(int ID){
        SQLiteDatabase myDB = this.getReadableDatabase();
        String[] mySearch = new String[]{String.valueOf(ID)};
        Cursor myCursor = myDB.rawQuery("SELECT "+ fItemIsLocked +" FROM "+ tTableName +" WHERE "+ fItemID +"=?",mySearch);
        myCursor.moveToFirst();
        int index = myCursor.getColumnIndex(fItemID);
        String myAnswer = myCursor.getString(index);
        myCursor.close();
        return myAnswer;
    }
    public int numberOfLives(){
        int ID = 1;
        SQLiteDatabase myDB = this.getReadableDatabase();
        String[] mySearch = new String[]{String.valueOf(ID)};
        Cursor myCursor = myDB.rawQuery("SELECT "+ fItemNumberOfPurchases +" FROM "+ tTableName +" WHERE "+ fItemID +"=?",mySearch);
        myCursor.moveToFirst();
        int index = myCursor.getColumnIndex(fItemNumberOfPurchases);
        myCursor.close();
        return index;
    }
}








Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 commentaires:

Enregistrer un commentaire

 

© 2013 Mobile Probleme. All rights resevered. Designed by Templateism

Back To Top