So, here is the question, I have two classes, Class Title the information of a person, i.e name, address, website, phone etc and when the person selects save button all his personal data is saved in SQlite and he is moved to Class Newscene which have a edit text view on which the person writes a story. and as he writes the story, i want it to get saved against the id of the title which has been saved recently. but instead of doing it, the program crashes and gives me error.here is the code of my title class from which i am inserting the details of a person into database
EditText title=(EditText)findViewById(R.id.editText1);
EditText author=(EditText)findViewById(R.id.editText2);
EditText company=(EditText)findViewById(R.id.editText3);
EditText address=(EditText)findViewById(R.id.editText4);
EditText email=(EditText)findViewById(R.id.editText5);
EditText website=(EditText)findViewById(R.id.editText6);
EditText phone=(EditText)findViewById(R.id.editText7);
title1=title.getText().toString();
author1=author.getText().toString();
company1=company.getText().toString();
address1=address.getText().toString();
email1=email.getText().toString();
website1=website.getText().toString();
phone1=phone.getText().toString();
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact(0, title1, author1,company1, address1, email1, website1, phone1, script1));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.get_name() + " ,Author: " + cn.get_author() + " ,company: " + cn.get_company() + " ,address: " + cn.get_address()
+ " ,email: " + cn.get_email() + " ,Website: " + cn.get_website() + " ,Phone: " + cn.getPhoneNumber() + " ,Script: " + cn.getscript();
// Writing Contacts to log
Log.d("Name: ", log);
name_title=cn.getName().toString();
}
//Passing id through intent
Intent i = new Intent(Title.this,Newscene.class);
i.putExtra("EXTRA_ID", id );
startActivity(i);
This is where i am getting it in Newscene Class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newscene);
Intent i = getIntent();
idval = i.getStringExtra("EXTRA_ID").toString();
// i am converting this idval string to integer below
}
then in same class, everything a person writes in the edittext it saves immediately in
String strSaveindb = previous + scene1 + ending;
and then i am setting this in my Contact class and updating database like this ( this is being done in Newscene activity )
cn.setscript(strSaveindb);
db.updateContactscript(db.getContact(1));
Now in my Databasehandler class i have updatecontact function and updatecontactscript function, following is the code of my database handler class
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " TEXT,"
+ KEY_AUTHOR + " TEXT,"
+ KEY_COMPANY + " TEXT,"
+ KEY_ADDRESS + " TEXT,"
+ KEY_EMAIL + " TEXT,"
+ KEY_WEBSITE + " TEXT,"
+ KEY_PH_NO + " TEXT,"
+ KEY_SCRIPT + "TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
tHIS IS THE ADD CONTACT FUNCTION
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_AUTHOR, contact.getPhoneNumber());
values.put(KEY_COMPANY, contact.getPhoneNumber());
values.put(KEY_ADDRESS, contact.getPhoneNumber());
values.put(KEY_EMAIL, contact.getPhoneNumber());
values.put(KEY_WEBSITE, contact.getPhoneNumber());
values.put(KEY_PH_NO, contact.getPhoneNumber());
values.put(KEY_SCRIPT, contact.getscript());// Contact Phone
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
This is the get conatct function
Contact getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_AUTHOR, KEY_COMPANY, KEY_ADDRESS, KEY_EMAIL, KEY_WEBSITE, KEY_PH_NO , KEY_SCRIPT }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getString(7),cursor.getString(8));
// return contact
return contact;
}
And these are the functions to update contact, and update only script
// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_AUTHOR, contact.get_author());
values.put(KEY_COMPANY, contact.get_company());
values.put(KEY_ADDRESS, contact.get_address());
values.put(KEY_EMAIL, contact.get_email());
values.put(KEY_WEBSITE, contact.get_website());
values.put(KEY_PH_NO, contact.getPhoneNumber());
values.put(KEY_SCRIPT, contact.getscript());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
//UPDATING SCRIPT
public int updateContactscript(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SCRIPT, contact.getscript());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
How Can i update the script infront of the id of the contact which have recently been made. i am stuck here for two days now :(
"TEXT". I think you are the fifth I see today.