java - How to fetch SQLite data from selected Spinner item in Android -
i have retrieved first column data in spinner
. want retrieve sqlite data when select spinner item. data displayed in edittext
s. when run app, data cannot displayed in edittext
. can give me suggestion?thanks in advance.
here code
spinner_searchbyempname = (spinner)findviewbyid(r.id.searchbyempname); loadserachbyproject() ; spinner_searchbyempname = (spinner)findviewbyid(r.id.searchbyempname); loadserachbyproject() ; spinner_searchbyempname.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub selectedemployeename = spinner_searchbyempname.getselecteditem().tostring().trim(); system.out.println("selectedprojectname " + selectedemployeename); } @override public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub } }); etempname=(edittext)findviewbyid(r.id.delete_edittext_staffemployee_name); etdepartment=(edittext)findviewbyid(r.id.delete_edittext_department); etdesignation=(edittext)findviewbyid(r.id.delete_edittext_designation); try { databasehelper = new databasehelper(this); db=databasehelper.getreadabledatabase(); cursor cursor = db.rawquery("select staff_emp_name, department, designation employee_details staff_emp_name = ?", new string[] { "" + selectedemployeename }); if(cursor!=null && cursor.movetofirst()) { etempname.settext(cursor.getstring(cursor .getcolumnindex("staff_emp_name"))); etdepartment.settext(cursor.getstring(cursor .getcolumnindex("department"))); etdesignation.settext(cursor.getstring(cursor .getcolumnindex("designation"))); } } catch (exception e) { // todo: handle exception e.printstacktrace(); } }
i suggest take @ tutorial in android database here take look: android sqlite database , content provider - tutorial
never use hardcode name table name , columns store them in variables one:
public static final string emp_table = "employee_details"; public static final string emp_col_name = "staff_emp_name"
you can create object representing data in database one:
public class employee{ private string name; private string department; private string designation; public employee(string name,string dept,string designation){ this.name = name; this.department = dept; this.designation = designation; } public string getdepartment() { return department; } public void setdepartment(string department) { this.department = department; } public string getdesignation() { return designation; } public void setdesignation(string designation) { this.designation = designation; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
also refactor method 1 below:
private employee getemployeebyproj(string name){ try { databasehelper = new databasehelper(this); db=databasehelper.getreadabledatabase(); final string = emp_table + "=\"" + name + "\""; cursor cursor = db.query("employee_details",new string[] {"staff_emp_name","department","designation"},where,null,null,null,null); if(cursor!=null && cursor.movetofirst()) { final string name = cursor.getstring(cursor .getcolumnindex(emp_col_name)); final string dept = cursor.getstring(.... final string designation cursor.getstring(..... //do rest of code here return new employee(name,dept,designation); } } catch (exception e) { // todo: handle exception e.printstacktrace(); } } }
i suggest take on link given above
Comments
Post a Comment