我需要知道如何默认情况下(当您第一次将数据加载应用程序时)将数据插入到我的sqlite数据库中,例如
gameidno :1 gamedate :2014年11月30日 gameteam :马刺
gameopponent :LAL
gameidno :2
的 gamedate :2014年5月12日
的 gameteam :马刺
gameopponent :小牛
gameidno :3
的 gamedate :2014年9月12日
的 gameteam :马刺
gameopponent :孟菲斯
这是我的DBhelper:
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE if not exists gamestable(id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "gameidno"
+ " INTEGER,"
+ "gamedate"
+ " TEXT,"
+ "gameteam"
+ " TEXT,"
+ "gameopponent" + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + gamestable);
onCreate(db);
}
public void addGame(GameModel gameitem) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("gameidno", gameitem.gameidno);
contentValues.put("gamedate", gameitem.gamedate );
contentValues.put("gameteam", gameitem.gameteam);
contentValues.put("gameopponent", gameitem.gameopponent);
db.insert("gamestable", null, contentValues);
db.close();
}
这是我的班级:
public class AddGames extends Activity implements OnClickListener {
private Button btn_addrecord;
private EditText txtpname;
DatabaseHelper db;
GameModel pm;
private Spinner gamesSpinner;
private Spinner timeSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_reminder);
txtpname = (EditText) findViewById(R.id.myEditText);
gamesSpinner = (Spinner) findViewById(R.id.games_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.reminders_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gamesSpinner.setAdapter(adapter);
timeSpinner = (Spinner) findViewById(R.id.time_spinner);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,
R.array.time_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
timeSpinner.setAdapter(adapter1);
btn_addrecord = (Button) findViewById(R.id.button1);
btn_addrecord.setOnClickListener(this);
}
@Override
public void onClick(View v) {
DatabaseHelper db;
ProductModel pm;
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
if (txtpname.getText().toString().equals("")
|| gamesSpinner.getSelectedItem().toString().equals("")) {
Toast.makeText(AddGames.this, "Please add values..",
Toast.LENGTH_LONG).show();
} else {
db = new DatabaseHelper(getApplicationContext());
db.getWritableDatabase();
pm = new GamesModel();
pm.idno = (txtpname.getText().toString());
pm.gamename = gameSpinner.getSelectedItem().toString();
pm.reminderway = timeSpinner.getSelectedItem().toString();
Log.i("gameidno,gamename,productprice", "" + pm.idno + ""
+ pm.gamename + "" + pm.reminderway);
db.addGame(pm);
Toast.makeText(Addgame.this, "Record Added successfully.",
Toast.LENGTH_LONG).show();
finish();
}
break;
default:
break;
}
}
GameModel类:
public class GameModel {
public String gameidno="", gamedate="", gameteam="", gameopponent="";
public String getgamedate() {
return gamedate;
}
public void setgamedate(String gamedate) {
this.gamedate = gamedate;
}
public String getgameteam() {
return gameteam;
}
public void setgameteam(String gameteam) {
this.gameteam = gameteam;
}
public String getgameopponent() {
return gameopponent;
}
public void setgameopponent(String gameopponent) {
this.gameopponent = gameopponent;
}
public String getgameIdno() {
return gameidno;
}
public void setgameIdno(String idno) {
this.gameidno = gameidno;
}
}
答案 0 :(得分:1)
您需要在oncreate方法中维护一个首选项变量,如下面所示:
public class AddGames extends Activity implements OnClickListener {
SharedPreferences pref;
private Button btn_addrecord;
private EditText txtpname;
DatabaseHelper db;
GameModel pm;
private Spinner gamesSpinner;
private Spinner timeSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_reminder);
pref=getSharedPreferences("your_pref_name", MODE_PRIVATE);
boolean firstTime=pref.getBoolean("first_launch", false);
if(firstTime == false){
pm = new GamesModel();
pm.idno = "your_id";
pm.gamename = "your_game_name";
pm.reminderway = "your_reminder";
pm.addGame(pm);
Toast.makeText(Addgame.this, "Fisrt Record Added successfully.",
Toast.LENGTH_LONG).show();
SharedPreference.Editor ed=pref.edit();
pref.putBoolean("first_launch", true);
ed.commit();
}
}
答案 1 :(得分:1)
只需将此构造函数放入GameModel
类。
public GameModel (String gameidno,String gamedate,String gameteam,String gameopponent){
this.gameidno= gameidno;
this.gamedate= gamedate;
this.gameteam= gameteam;
this.gameopponent= gameopponent;
}
在您要添加值的活动中,只需使用以下代码即可。
db.addGame(new GameModel(yourvalue, yourvalue,yourvalue,yourvalue));
我相信它会添加到您的sqlite
中。