I have 1 Spinner, 4 Edittext and a Button. The save button saves the information into a SQLite database. Oh, I get "force close" message.
I need help thanks. Below are the codes of 2 classes.
PersonalInfo class
public class PersonalInfo extends Activity
{
Button btnSave, btnBack;
EditText txtLikes, txtDislikes, txtDate, txtType;
Spinner nameSpinner;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*try{
String destPath = "/data/data" + getPackageName() +
"/databases/AnniversaryDB";
File f = new File(destPath);
if(!f.exists())
{
CopyDB(getBaseContext().getAssets().open("AnniversaryDB"),
new FileOutputStream(destPath));
}
}catch (FileNotFoundException e)
{
e.printStackTrace();
}catch (IOException e)
{
e.printStackTrace();
}*/
InfoDBAdapter dbA = new InfoDBAdapter(this);
// ---add a contact---
dbA.open();
long id = dbA.insertContact("Kanak Priya", "24/6/1990", "Birthday", "Painting", "Bungee jumping");
id = dbA.insertContact("Joanne Liew", "15/8/1990", "Birthday", "Skating", "Yoga");
dbA.close();
// ---get all contacts---
dbA.open();
Cursor c = dbA.getAllContacts();
if (c.moveToFirst())
{
do
{
DisplayContact(c);
} while (c.moveToNext());
}
dbA.close();
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Spinner nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
String NameValue = nameSpinner.getSelectedItem().toString();
EditText txtDate = (EditText) findViewById(R.id.txtDate);
String DateValue = txtDate.getText().toString();
EditText txtType = (EditText) findViewById(R.id.txtType);
String TypeValue = txtType.getText().toString();
EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
String LikesValue = txtLikes.getText().toString();
EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);
String DislikesValue = txtDislikes.getText().toString();
Toast.makeText(getBaseContext(),
"Your info is saved successfully!",
Toast.LENGTH_SHORT).show();
}
});
btnBack = (Button) findViewById(R.id.btnMain);
btnBack.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
}
});
/*
* //---get a contact--- db.open(); Cursor c = db.getContact(2); if
* (c.moveToFirst()) DisplayContact(c); else Toast.makeText(this,
* "No contact found", Toast.LENGTH_LONG).show(); db.close();
*/
/*
* //---update contact--- db.open(); if (db.updateContact(1,
* "Kanak Priya", "24/6/1990", "Birthday", "Painting", "Bungee jumping")) Toast.makeText(this,
* "Update successful.", Toast.LENGTH_LONG).show(); else
* Toast.makeText(this, "Update failed.", Toast.LENGTH_LONG).show();
* db.close();
*/
/*
* //---delete a contact--- db.open(); if (db.deleteContact(1))
* Toast.makeText(this, "Delete successful.", Toast.LENGTH_LONG).show();
* else Toast.makeText(this, "Delete failed.",
* Toast.LENGTH_LONG).show(); db.close();
*/
}// end onCreate()
/*public void CopyDB(InputStream inputStream,
OutputStream outputStream)
throws IOException
{
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}*/
public void DisplayContact(Cursor c)
{
Toast.makeText(
this,
"id: " + c.getString(0) + "\n" + "Name: " + c.getString(1)
+ "\n" + "Date: " + c.getString(2) + "\n" + "Type: " + c.getString(3) + "\n" + "Likes: " + c.getString(4)
+ "\n" + "Dislikes: " + c.getString(5), Toast.LENGTH_LONG)
.show();
}
}
InfoDBAdapter class
public class InfoDBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_DATE = "date";
public static final String KEY_TYPE = "type";
public static final String KEY_LIKES = "likes";
public static final String KEY_DISLIKES = "dislikes";
private static final String TAG = "InfoDBAdapter";
private static final String DATABASE_NAME = "AnniversaryDB";
private static final String DATABASE_TABLE = "personalInfo";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE = "create table personalInfo (_id integer primary key autoincrement, "
+ "name text not null, date numeric not null, type text not null" +
"likes text not null, dislikes text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public InfoDBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);
} catch (SQLException e)
{
e.printStackTrace();
}
}// end onCreate()
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS personalInfo");
onCreate(db);
}// end onUpgrade()
}// end DatabaseHelper
// ---opens the database---
public InfoDBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}// end open()
// ---closes the database---
public void close()
{
DBHelper.close();
}// end close()
// ---insert a contact into the database---
public long insertContact(String name, String date, String type, String likes, String dislikes)
{
ContentValues values = new ContentValues();
values.put(KEY_NAME, name);
values.put(KEY_DATE, date);
values.put(KEY_TYPE, type);
values.put(KEY_LIKES, likes);
values.put(KEY_DISLIKES, dislikes);
return db.insert(DATABASE_TABLE, null, values);
}// end insertContact()
// ---deletes a particular contact---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}// end deleteContact()
// ---retrieves all the contacts---
public Cursor getAllContacts()
{
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME,
KEY_DATE, KEY_TYPE, KEY_LIKES, KEY_DISLIKES }, null, null, null, null, null);
}// end getAllContacts()
// ---retrieves a particular contact---
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_NAME, KEY_DATE, KEY_TYPE, KEY_LIKES, KEY_DISLIKES }, KEY_ROWID + "=" + rowId,
null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}// end getContact()
// ---updates a contact---
public boolean updateContact(long rowId, String name, String date, String type, String likes, String dislikes)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_DATE, date);
args.put(KEY_TYPE, type);
args.put(KEY_LIKES, likes);
args.put(KEY_DISLIKES, dislikes);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}// end updateContact()
}//end DBAdapter