i have a functional DBhelper class extended to SQLiteOpenhelper which successfully created and populates my database. However when i try to populate the database with checked box values my app crashes. I believe its something wrong with my piece of code which says controller.insert_checkbox(checked), because when i comment it out my code runs and shows the toast that follows on the screen. Here is my code;
public class LocationCategoryChkbxNew extends AppCompatActivity {
Button button2;
CheckBox chkbox15;
int checked = 0;
ResturantsDbHelper controller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_category_chkbx_new);
controller = new ResturantsDbHelper(this);
button2 = (Button)findViewById(R.id.button2);
getSupportActionBar().setTitle("Your Foodometer: Hungry ..");
chkbox15=(CheckBox)findViewById(R.id.checkBox15);
chkbox15.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checked = 1;
}else {
checked = 0;
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
controller.insert_checkbox(checked);
Toast.makeText(getApplicationContext(), "Saved '"+ checked + "' in DB", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(LocationCategoryChkbxNew.this,
ResturantCategoryChkboxNew.class);
startActivity(myIntent);
}
});
}
And here is my DBHelper class public class ResturantsDbHelper extends SQLiteOpenHelper { private static final String TAG ="DatabaseHelper";
// FIRST DB - Name of the resturant database file. if you change the DB Schema you must increment the database version
private static final String DATABASE_NAME1 = "Resturants.db";
//Resturant Database version
private static final int DATABASE_VERSION1 = 1;
//import LocationCategoryChkbx class
//private LocationCategoryChkbx;
public ResturantsDbHelper(Context context){
super(context, DATABASE_NAME1,null, DATABASE_VERSION1);
}
//watching the spacing, you cannot leave spaces after the comma and quote at the end of each statement below
@Override
public void onCreate(SQLiteDatabase db) {
String SQL_CREATE_RESTURANTS_TABLE = "CREATE TABLE " + ResturantEntry.TABLE_NAME + "("
+ ResturantEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ ResturantEntry.COLUMN_NAME + " TEXT NOT NULL,"
+ ResturantEntry.COLUMN_TYPE + " TEXT NOT NULL,"
+ ResturantEntry.COLUMN_ADDRESS + " TEXT NOT NULL,"
+ ResturantEntry.COLUMN_LOCATION + " TEXT NOT NULL,"
+ ResturantEntry.COLUMN_GPSCOORDINATES + " INTEGER NOT NULL,"
+ ResturantEntry.COLUMN_TELEPHONE + " INTEGER NOT NULL,"
+ ResturantEntry.COLUMN_OPENINGHOURS + " INTEGER NOT NULL,"
+ ResturantEntry.COLUMN_CLOSINGHOURS + " INTEGER NOT NULL,"
+ ResturantEntry.COLUMN_CHECKBOX + " TEXT);";
db.execSQL(SQL_CREATE_RESTURANTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL(String.format("DROP IF TABLE EXISTS %s", TABLE_NAME));
onCreate(db);
}
//We create a method for adding data to the database, we will call it “Insert_data” //public boolean insert_data(String s, String trim, String item, String trim1, String s1, String trim2, String s2, String trim3){ public void insert_data (String mResturantName, String mAddress, String mRestTypeSpinner, String mLocationSpinner, String mGPS_coordinates, String mTelephone, String mOpeningHour, String mClosingHour){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(ResturantContract.ResturantEntry.COLUMN_NAME, mResturantName ); contentValues.put(ResturantContract.ResturantEntry.COLUMN_TYPE, mAddress); contentValues.put(ResturantContract.ResturantEntry.COLUMN_ADDRESS, mRestTypeSpinner); contentValues.put(ResturantContract.ResturantEntry.COLUMN_LOCATION, mLocationSpinner); contentValues.put(ResturantContract.ResturantEntry.COLUMN_GPSCOORDINATES, mGPS_coordinates); contentValues.put(ResturantContract.ResturantEntry.COLUMN_TELEPHONE, mTelephone); contentValues.put(ResturantContract.ResturantEntry.COLUMN_OPENINGHOURS, mOpeningHour); contentValues.put(ResturantContract.ResturantEntry.COLUMN_CLOSINGHOURS, mClosingHour);
this.getWritableDatabase().insertOrThrow(ResturantContract.ResturantEntry.TABLE_NAME,"",contentValues);
}
public Cursor getResturantList(){
SQLiteDatabase database=this.getWritableDatabase();
Cursor resturantdata;
resturantdata = database.rawQuery("SELECT * FROM " + TABLE_NAME,null);
return resturantdata;
}
public void insert_checkbox(int checked) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ResturantContract.ResturantEntry.COLUMN_CHECKBOX, checked );
this.getWritableDatabase().insertOrThrow(ResturantContract.ResturantEntry.TABLE_NAME,"",contentValues);
}
}
I will appreciate any help, I have been battling this for 2 days now
Aucun commentaire:
Enregistrer un commentaire