sqlite - Android - Two spinners in one Activity - Saving Data is not working -
my problem following. have 2 spinners in 1 activity. want both spinner items should saved in db. there slight difference between both spinners:
- spinner 1: populated data other db-table - when spinner in mentioned activity, works.
- spinner 2: has default values. not sure if added correctly activity. anyway, after added saving data not working anymore.
please me doing wrong??
my activity code:
public class activity_transport extends appcompatactivity { private bottomnavigationviewex bottomnavigationviewex; databasehelper mydb; button btn_save; spinner chooseproject,choosetransport; edittext entfernung,price,mwst; string selectedspinner, selectedtransport; imageview imageview2; private static int pick_image = 100; uri imageuri; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_transport); mydb = new databasehelper(this); chooseproject = (spinner) findviewbyid(r.id.chooseproject); choosetransport = (spinner) findviewbyid(r.id.choosetransport); entfernung = (edittext) findviewbyid(r.id.entfernung); price = (edittext) findviewbyid(r.id.preis); mwst = (edittext) findviewbyid(r.id.mwst); btn_save=(button) findviewbyid(r.id.btn_save); //bildanzeigebereich im layout imageview2=(imageview) findviewbyid(r.id.imageview2); loadprojectspinnerdata(); //loadtransportspinnerdata(); savedata(); //+++++++++++++++function load spinner data sqlite database++++++++++// private void loadprojectspinnerdata() { // database handler databasehelper db = new databasehelper (getapplicationcontext()); // spinner drop down elements list<string> projects = db.getallprojects(); // creating adapter spinner arrayadapter<string> dataadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, projects); // drop down layout style - list view radio button dataadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); chooseproject.setprompt("projekt auswählen"); // attaching data adapter spinner chooseproject.setadapter(dataadapter); //choose transport spinner arrayadapter<string> spinnerarrayadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, new string[] { new string("auto"), new string("flugzeug"), new string("taxi"), new string("bahn")}); choosetransport.setadapter(spinnerarrayadapter); // drop down layout style - list view radio button spinnerarrayadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); choosetransport.setprompt("transport auswählen"); // attaching data adapter spinner choosetransport.setadapter(spinnerarrayadapter); //listener für den projekt spinner damit ich den wert abspeichern kann chooseproject.setonitemselectedlistener(new adapterview.onitemselectedlistener() { public void onitemselected(adapterview<?> parent, view view, int pos, long id) { spinner spinner = (spinner) parent; if(spinner.getid() == r.id.chooseproject) { selectedspinner = (string) chooseproject.getselecteditem(); } else if(spinner.getid() == r.id.choosetransport) { selectedtransport = (string) choosetransport.getselecteditem(); } //selectedspinner =string.valueof(parent.getitematposition(pos)); } public void onnothingselected(adapterview<?> parent) { } }); } /*private void loadtransportspinnerdata() { // spinner method read on selected value arrayadapter<string> spinnerarrayadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, new string[] { new string("auto"), new string("flugzeug"), new string("taxi"), new string("bahn")}); choosetransport.setadapter(spinnerarrayadapter); // creating adapter spinner //arrayadapter<string> dataadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, projects); // drop down layout style - list view radio button spinnerarrayadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); choosetransport.setprompt("transport auswählen"); // attaching data adapter spinner choosetransport.setadapter(spinnerarrayadapter); //listener für den projekt spinner damit ich den wert abspeichern kann choosetransport.setonitemselectedlistener(new adapterview.onitemselectedlistener() { public void onitemselected(adapterview<?> parent, view view, int pos, long id) { //selectedspinner =string.valueof(parent.getitematposition(pos)); selectedtransport = (string) choosetransport.getselecteditem(); } public void onnothingselected(adapterview<?> parent) { } }); }*/ //++++++++++++save data++++++// public void savedata(){ btn_save.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { boolean isinserted = mydb.createtransport( selectedspinner, selectedtransport, integer.valueof(price.gettext().tostring()), integer.valueof(mwst.gettext().tostring()), integer.valueof(entfernung.gettext().tostring()), imageviewtobyte(imageview2) ); if(isinserted=true) toast.maketext(activity_transport.this, "daten gespeichert", toast.length_long).show(); else toast.maketext(activity_transport.this, "daten nicht gespeichert", toast.length_long).show(); } } ); } }
edit:
i tried run app setting few breakpoints. when trying run line selectedtransport = (string) choosetransport.getselecteditem();
debugger posts cannot find local variable 'choosetransport'
i solved problem:
i implemented own method choosetransport
- spinner , works. figured out order how add data db not right. changed too.
see here method second spinner:
private void loadtransportspinnerdata(){ arrayadapter<charsequence> spinnerarrayadapter = arrayadapter.createfromresource(this, r.array.transport, android.r.layout.simple_spinner_dropdown_item); spinnerarrayadapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); choosetransport.setprompt("transport auswählen"); choosetransport.setadapter(spinnerarrayadapter); choosetransport.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { selectedtransport = (string) choosetransport.getselecteditem(); } @override public void onnothingselected(adapterview<?> adapterview) { } }); }
Comments
Post a Comment