I've met an unknown error while inserting data into the database. The LogCat had been display "unable to insert data", however, the code that I've used is copied from another activity (which works).
weird enough, when I placed the code in the onStart() method, it works. but when I placed into my onClick(), it failed.
onStart(): <- not sure whether it's related to the error
@Override
public void onStart() {
super.onStart();
dbopener = new DB_Route_ListOpener(this);
try {
dbopener.createDatabase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}//create database
try {
dbopener.openDatabase();
}catch(SQLException sqle){
throw sqle;
}//open database
// Configure the listview
routeItems = new ArrayList<String>(dbopener.selectData(new String [] {"RouteName"}, null, null, null, null, null));
routeListAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,routeItems);
routeListItem = (ListView)this.findViewById(R.id.mlt_route_list_route);
routeListItem.setAdapter(routeListAdapter);
routeListItem.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
dbopener.close();
}//onStart
onClick():
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == findViewById(R.id.mlt_route_btn_addroute))//add
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Add name ?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
dialog.dismiss();
ContentValues cv = new ContentValues();
cv.put("RouteName", "test");
dbopener.insertData(null, cv);
}
});//clear all data
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
dialog.cancel();
}
});//negative
builder.create().show();
}//if
my insert query:
public void insertData (String nullColumnHack, ContentValues values) {
try
{
myDatabase.insert(DB_TABLE, nullColumnHack, values);
} catch (Exception e) {
Log.e("Error :","unable to insert data");
}//catch
}//insertData
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)