views:

49

answers:

1

So basically my app starts out with a TabView, then through the options menu the user selects to add a game to the database. This opens the InputGame class, which accepts a few values, and then it should put them into a database when the user clicks "Submit". It will also go back to the original home view. The app goes back the the home tabs view just fine, bu nothing ever gets added to the database. Am I doing something wrong?

InputGame:

public class InputGame extends Activity {

    private EditText gameTitle;
    private Spinner consoleSelect;
    private Spinner genreSelect;
    private Spinner ratingSelect;
    private Spinner styleSelect;
    private EditText gamePub;
    private EditText gameDev;
    private EditText gameRegion;
    private EditText gamePrice;
    private EditText gameRelease; 

    private DatabaseHelper db = null;
    private Cursor constantsCursor=null;
    private Button addGame;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inputgame);

        db = new DatabaseHelper(this);
        constantsCursor = db.getReadableDatabase().rawQuery(
                "SELECT _ID, title, console " + "FROM constants ORDER BY title",
                null);


        /*Hide the scroll bar*/
        ScrollView sView = (ScrollView)findViewById(R.id.ScrollInput);
        sView.setVerticalScrollBarEnabled(false);
        sView.setHorizontalScrollBarEnabled(false);

        /* Accepts game title */
        gameTitle = (EditText) findViewById(R.id.gametitle);        

        /* Console select*/ 
        consoleSelect = (Spinner) findViewById(R.id.console_select);
        ArrayAdapter<CharSequence> consoleAdapter = ArrayAdapter.createFromResource(
                this, R.array.consoles_array, android.R.layout.simple_spinner_item);
        consoleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        consoleSelect.setAdapter(consoleAdapter);


        /* Genre select*/   
        genreSelect = (Spinner) findViewById(R.id.genre_select);
        ArrayAdapter<CharSequence> genreAdapter = ArrayAdapter.createFromResource(
                this, R.array.genres_array, android.R.layout.simple_spinner_item);
        genreAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        genreSelect.setAdapter(genreAdapter);

        /* Style  select*/  
        styleSelect = (Spinner) findViewById(R.id.style_select);
        ArrayAdapter<CharSequence> styleAdapter = ArrayAdapter.createFromResource(
                this, R.array.style_array, android.R.layout.simple_spinner_item);
        styleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        styleSelect.setAdapter(styleAdapter);

        /* Rating select*/  
        ratingSelect = (Spinner) findViewById(R.id.rating_select);
        ArrayAdapter<CharSequence> ratingAdapter = ArrayAdapter.createFromResource(
                this, R.array.ratings_array, android.R.layout.simple_spinner_item);
        ratingAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ratingSelect.setAdapter(ratingAdapter);

        /* Accepts game publisher */
        gamePub = (EditText) findViewById(R.id.gamepublisher);

        /* Accepts game developer */
        gameDev = (EditText) findViewById(R.id.gamedev);




        /* Release date selector */
        gameRelease = (EditText) findViewById(R.id.gamerelease);

        /* Add the game button */
        this.addGame = (Button)this.findViewById(R.id.addgame);
        this.addGame.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                processAdd();
            }


        });


    }
    private void processAdd(){
        ContentValues values=new ContentValues(8);

        values.put("title", getGameTitle());
        values.put("console", getGameConsole());
        values.put("genre", getGameGenre());
        values.put("style", getGameStyle());
        values.put("rating", getGameRating());
        values.put("publisher", getGamePublisher());
        values.put("developer", getGameDeveloper());
        values.put("release", getGameRelease());

        db.getWritableDatabase().insert("constants", "title", values);
        constantsCursor.requery();
        Intent launchgamesActivity = new Intent(this, GCM.class);
        startActivity(launchgamesActivity);
    }
    public String getGameTitle(){
        return(gameTitle.getText().toString());
    }
    public String getGameConsole(){
        return (consoleSelect.getContext().toString());
    }
    public String getGameGenre(){
        return(genreSelect.getContext().toString());
    }
    public String getGameStyle(){
        return(styleSelect.getContext().toString());
    }
    public String getGameRating(){
        return(ratingSelect.getContext().toString());
    }
    public String getGamePublisher(){
        return(gamePub.getText().toString());
    }
    public String getGameDeveloper(){
        return(gameDev.getText().toString());
    }
    public String getGameRelease(){
        return(gameRelease.getText().toString());
    }}

and here is my database helper. The records I have pre-programmed in show up just fine:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "gameDb";
    public static final String TITLE="title";
    public static final String CONSOLE="console";
    public static final String GENRE="genre";
    public static final String RATING="rating";
    public static final String PUBLISHER="publisher";
    public static final String DEVELOPER="developer";
//  public STATIC FINAL STRING REGION="REGION";
    public static final String PRICE="price";
    public static final String RELEASE="release";
    public static final String COMPLETION="completion";
    public static final String DESCRIPION="description";
    public static final String IMAGE="image";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db
                .execSQL("CREATE TABLE constants (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, console TEXT, genre TEXT, " +
                "rating TEXT, publisher TEXT, developer TEXT, region TEXT, price REAL, release BLOB, completion BLOB, description BLOB, image BLOB);");

        ContentValues cv = new ContentValues();

        cv.put(TITLE, "Super Mario Galaxy");
        cv.put(CONSOLE, "Wii");
        db.insert("constants", TITLE, cv);

        cv.put(TITLE, "Transformers: War for Cybertron");
        cv.put(CONSOLE, "Xbox 360");
        db.insert("constants", TITLE, cv);

        cv.put(TITLE, "Final Fantasy XIII");
        cv.put(CONSOLE, "Playstation 3");
        db.insert("constants", TITLE, cv);

        cv.put(TITLE, "Final Fantasy VII");
        cv.put(CONSOLE, "Playstation");
        db.insert("constants", TITLE, cv);

        cv.put(TITLE, "New Super Mario Bros");
        cv.put(CONSOLE, "Nintendo DS");
        db.insert("constants", TITLE, cv);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        android.util.Log.w("Constants",
                "Upgrading database, which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS constants");
        onCreate(db);
    }
}

EDIT: When submitting from InputGames, should I be closing that activity all together? I think the way I have it set now just goes back to the other activity, but nothing really gets done. Or is that not it at all?

EDIT #2 Well I got it to add. For some reason I have to use .execSQL. Thats fine I guess, but bow when I try to add from a spinner item in InputGames, I get this populating the respective element:

com.jvavrik.gcm.InputGame@43bb0510

were the number after the "@" is always different. Any ideas for this?

+1  A: 

What if you change this:

db.getWritableDatabase().insert("constants", "title", values);

to this:

db.getWritableDatabase().insert("constants", null, values);
Cristian
Nope, still nothing. The same thing happens.
James