android - java.lang.ClassCastException error when trying to save data in the sqlite on button click -
i have tab in activity when click on button open tab view works fine. after entering data in fields when click on button save it gives me error.may have error in sqlite database constructor not sure.some 1 please help: code tab:
package com.example.doctormanager;
import android.app.activity; import android.content.contentvalues; import android.content.context; import android.content.intent; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.imagebutton; import android.widget.listview; public class general extends activity { context context; private button proceed; private edittext patient_name,mobile,address,email,dob,age; listview template_list; intent data_intent; string name=""; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.patient_general); data_intent=getintent(); name=data_intent.getstringextra("name"); patient_name=(edittext)findviewbyid(r.id.txt17); patient_name.settext(""+name); mobile=(edittext)findviewbyid(r.id.txt23); address=(edittext)findviewbyid(r.id.txt21); email=(edittext)findviewbyid(r.id.txt19); dob=(edittext)findviewbyid(r.id.txt25); age=(edittext)findviewbyid(r.id.txt27); imagebutton save= (imagebutton)findviewbyid(r.id.ib13); save.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub sqlhandler db = new sqlhandler(this); string p_name = patient_name.gettext().tostring(); string p_mobile = mobile.gettext().tostring(); string p_addr = address.gettext().tostring(); string p_email = email.gettext().tostring(); string p_dob = dob.gettext().tostring(); string p_age = age.gettext().tostring(); /** * crud operations * */ // inserting contacts log.d("insert: ", "inserting .."); db.addcontact(new patient_entery(0, p_name, p_mobile, p_addr, p_email, p_dob, p_age, null)); } }); } }
and database:
package com.example.doctormanager; import java.util.arraylist; import java.util.list; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; import android.view.view.onclicklistener; public class sqlhandler extends sqliteopenhelper { public sqlhandler(context context, string name, cursorfactory factory, int version) { super(context, name, factory, version); // todo auto-generated constructor stub } // static variables // database version private static final int database_version = 1; // database name private static final string database_name = "doctormanager"; // contacts table name private static final string table_patient_general= "general"; // contacts table columns names private static final string key_id = "id"; private static final string key_patient_name = "patient_name"; private static final string key_email = "patient_email"; private static final string key_patient_addr = "patient_addr"; private static final string key_patient_contact_number = "patient_contact_number"; private static final string key_patient_dob = "patient_dob"; private static final string key_patient_age = "patient_age"; private static final string key_patient_sex = "patient_sex"; public sqlhandler (onclicklistener onclicklistener) { super((context) onclicklistener, database_name, null, database_version); } // public sqlhandler(onclicklistener onclicklistener) { // // todo auto-generated constructor stub // } // creating tables @override public void oncreate(sqlitedatabase db) { string create_patient_table = "create table " + table_patient_general + "(" + key_id + " integer primary key," + key_patient_name + " text," + key_patient_contact_number + " text," + key_email +"text," + key_patient_addr + "text," + key_patient_dob + "text," + key_patient_age + "text," + key_patient_sex + "text " + ")"; db.execsql(create_patient_table); } // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // drop older table if existed db.execsql("drop table if exists " + table_patient_general); // create tables again oncreate(db); } /** * crud(create, read, update, delete) operations */ // adding new contact void addcontact(patient_entery patient) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_patient_name, patient.getname()); // contact name values.put(key_patient_contact_number, patient.getphonenumber()); // contact phone values.put(key_email, patient.getpatientemail()); // contact name values.put(key_patient_addr, patient.getaddress()); // contact phone values.put(key_patient_dob, patient.getdob()); // contact name values.put(key_patient_age, patient.getage()); // contact phone values.put(key_patient_sex, patient.getsex()); // contact name // inserting row db.insert(table_patient_general, null, values); db.close(); // closing database connection } // getting single contact patient_entery getpatient(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_patient_general, new string[] { key_id, key_patient_name, key_patient_contact_number, key_email, key_patient_addr, key_patient_dob, key_patient_age, key_patient_sex }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); patient_entery patient = new patient_entery(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)); // return contact return patient; } public string getallstringvalues() { arraylist<string> yourstringvalues = new arraylist<string>(); sqlitedatabase db = this.getreadabledatabase(); cursor result = db.query(true, table_patient_general, new string[] { key_patient_name }, null, null, null, null, null, null); if (result.movetofirst()) { { yourstringvalues.add(result.getstring(result .getcolumnindex(key_patient_name))); } while (result.movetonext()); } else { return null; } return key_patient_name; }
}
and xml file:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/login_back"> <linearlayout android:layout_height="30dip" android:layout_width="wrap_content" android:orientation="horizontal" > <textview android:id="@+id/txt16" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name: " android:textsize="20dip" android:textcolor="#ffffff"/> <edittext android:id="@+id/txt17" android:layout_width="200dp" android:layout_height="20dp" android:background="@drawable/edit_text_lines" android:ems="10" android:textsize="10dip" /> </linearlayout> <linearlayout android:layout_height="40dip" android:layout_width="wrap_content" android:orientation="horizontal"> <textview android:id="@+id/txt18" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="email: " android:textsize="20dip" android:textcolor="#ffffff"/> <edittext android:id="@+id/txt19" android:layout_width="200dp" android:layout_height="20dp" android:text="" android:textsize="10dip" android:background="@drawable/edit_text_lines" /> </linearlayout> <linearlayout android:layout_height="40dip" android:layout_width="wrap_content" android:orientation="horizontal"> <textview android:id="@+id/txt20" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="address: " android:textsize="20dip" android:textcolor="#ffffff"/> <edittext android:id="@+id/txt21" android:layout_width="200dp" android:layout_height="20dp" android:text="" android:textsize="10dip" android:background="@drawable/edit_text_lines" /> </linearlayout> <linearlayout android:layout_height="40dip" android:layout_width="wrap_content" android:orientation="horizontal"> <textview android:id="@+id/txt22" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="phone no.: " android:textsize="20dip" android:textcolor="#ffffff"/> <edittext android:id="@+id/txt23" android:layout_width="200dp" android:layout_height="20dp" android:text="" android:textsize="10dip" android:background="@drawable/edit_text_lines"/> </linearlayout> <linearlayout android:layout_height="40dip" android:layout_width="wrap_content" android:orientation="horizontal"> <textview android:id="@+id/txt24" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dob: " android:textsize="20dip" android:textcolor="#ffffff"/> <edittext android:id="@+id/txt25" android:layout_width="200dp" android:layout_height="20dp" android:text="" android:textsize="10dip" android:background="@drawable/edit_text_lines"/> </linearlayout> <linearlayout android:layout_height="40dip" android:layout_width="wrap_content" android:orientation="horizontal"> <textview android:id="@+id/txt26" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="age: " android:textsize="20dip" android:textcolor="#ffffff"/> <edittext android:id="@+id/txt27" android:layout_width="200dp" android:layout_height="20dp" android:text="" android:textsize="10dip" android:background="@drawable/edit_text_lines"/> </linearlayout> <linearlayout android:layout_height="40dip" android:layout_width="wrap_content" android:orientation="horizontal"> <textview android:id="@+id/stxt27" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sex: " android:textsize="20dip" android:textcolor="#ffffff"/> <radiogroup android:id="@+id/radiosex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:textcolor="#ffffff" > <radiobutton android:id="@+id/radiomale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="male" android:textcolor="#ffffff"/> <radiobutton android:id="@+id/radiofemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="female" android:textcolor="#ffffff" /> </radiogroup> </linearlayout> <textview android:id="@+id/txt28" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="patient/family history: " android:textsize="20dip" android:textcolor="#ffffff"/> <edittext android:id="@+id/edttxt27" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textcolor="#ffffff" /> <imagebutton android:id="@+id/ib14" android:src="@drawable/template" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingright="1dip" /> <imagebutton android:id="@+id/ib13" android:src="@drawable/next_visit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_gravity="center_horizontal" /> </linearlayout>
thanks in advance.
you need pass activity context
on sqlhandler
like:
sqlhandler db = new sqlhandler(youractivity.this);
in button
onclick()
and remove onclicklistener onclicklistener()
sqlhanlder constructer
like:
public sqlhandler (context con) { super(con, database_name, null, database_version); }
Comments
Post a Comment