所以基本上我的应用程序从TabView开始,然后通过选项菜单,用户选择将游戏添加到数据库。这将打开InputGame类,该类接受一些值,然后当用户单击“提交”时 将它们放入数据库中。它也将回到原始的主页视图。该应用程序返回主页标签视图就好了,没有任何东西被添加到数据库。我做错了吗?
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());
}}
这是我的数据库助手。我预编程的记录显示得很好:
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);
}
}
编辑:从InputGames提交时,我是否应该一起关闭该活动?我认为现在我设置它的方式只是回到其他活动,但没有真正完成。或者那不是吗?
编辑#2 我得到了补充。出于某种原因,我必须使用.execSQL。多数民众赞成我猜,但是当我尝试从InputGames中的一个微调器项添加时,我会鞠躬,我会填充相应的元素:
com.jvavrik.gcm.InputGame@43bb0510
是“@”总是不同之后的数字。有什么想法?
答案 0 :(得分:1)
如果你改变这个怎么办:
db.getWritableDatabase().insert("constants", "title", values);
到此:
db.getWritableDatabase().insert("constants", null, values);