我想在我的数据库中保存nbBeer,nbWhisky,nbWine,nbVodka,dateView以及我如何知道数据已保存?
nbBeer = (EditText) findViewById(R.id.nbBeer);
nbWhisky = (EditText) findViewById(R.id.nbWhisky);
nbWine = (EditText) findViewById(R.id.nbWine);
nbVodka = (EditText) findViewById(R.id.nbVodka);
//myDatePicker = (DatePicker) findViewById(R.id.myDatePicker);
dateView = (TextView) findViewById(R.id.dateView);
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
showDate(year, month+1, day);
dbHandler = new MyDBHandler(this, null, null, 1);
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
// arg1 = year
// arg2 = month
// arg3 = day
showDate(arg1, arg2+1, arg3);
}
};
private void showDate(int year, int month, int day) {
dateView.setText(new StringBuilder().append(day).append("/")
.append(month).append("/").append(year));
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
将产品添加到数据库中:
public void saveButtonClicked(View view){
Products product = new Products(nbBeer.getInputType(), dateView.getText().toString(), nbWhisky.getInputType(),nbWine.getInputType(), nbVodka.getInputType());
dbHandler.addProduct(product);
}
在我的DBHandler类中,我把:
public void onCreate(SQLiteDatabase db) {
String query = " CREATE TABLE" + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT" +
COLUMN_DATE + " TEXT" +
COLUMN_BEERS + "INTEGER" +
COLUMN_WHISKY + "INTEGER" +
COLUMN_WINE + "INTEGER" +
COLUMN_vodka + "INTEGER" +
")";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_PRODUCTS);
onCreate(db);
}
在数据库中添加新行:
public void addProduct(Products product){
ContentValues values = new ContentValues();
values.put(COLUMN_DATE, product.get_date());
values.put(COLUMN_BEERS, product.get_beers());
values.put(COLUMN_WHISKY, product.get_whisky());
values.put(COLUMN_WINE, product.get_wine());
values.put(COLUMN_vodka, product.get_vodka());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
和产品类
public class Products {
private int _id ;
private String _date;
private int _beers;
private int _whisky;
private int _wine;
private int _vodka;
public Products(){}
public Products(int beers, String date, int whisky, int wine, int vodka) {
this._beers = beers;
this._date = date;
this._whisky = whisky;
this._wine = wine;
this._vodka = vodka;
}
public int get_id() {
return _id;
}
public String get_date() {
return _date;
}
public int get_beers() {
return _beers;
}
public int get_whisky() {
return _whisky;
}
public int get_wine() {
return _wine;
}
public int get_vodka() {
return _vodka;
}
}
答案 0 :(得分:0)
public void addProduct(Products product){
try{
ContentValues values = new ContentValues();
values.put(COLUMN_DATE, product.get_date());
values.put(COLUMN_BEERS, product.get_beers());
values.put(COLUMN_WHISKY, product.get_whisky());
values.put(COLUMN_WINE, product.get_wine());
values.put(COLUMN_vodka, product.get_vodka());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_PRODUCTS, null, values);
db.close();
//To know that data add
Toast.makeText(getActivity(), "Everything OK =)",
Toast.LENGTH_LONG).show();
}catch (Exception e) {
e.printStackTrace();
}