how i can insert a spinner selected item in mysql php in android studio? -
i used 2 spinners 1 'username' , second 'course'. stored in db, retrieving spinners data mysql database works fine! want select item spinners, when click on submit button selected item should inserted mysql db.please give me php , java code. //java file
public class mainactivity_d3 extends appcompatactivity implements adapterview.onitemselectedlistener { //declaring spinner private spinner spinner2, spinner1; private string str_spinner1, str_spinner2, s_name, s_course; //an arraylist spinner items private arraylist<string> students1; private arraylist<string> students2; button mbtnsave; //json array private jsonarray result1, result2, result; //textviews display details private textview textviewname1; private textview textviewname2; private textview textviewcourse; private textview textviewsession; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mainactivity_d1); //initializing arraylist students1 = new arraylist<string>(); students2 = new arraylist<string>(); //initializing spinner //adding item selected listener our spinner //as have implemented class spinner.onitemselectedlistener class iteself passing setonitemselectedlistener spinner1 = (spinner) findviewbyid(r.id.spinner1); spinner2 = (spinner) findviewbyid(r.id.spinner2); spinner1.setonitemselectedlistener(this); spinner2.setonitemselectedlistener(this); // spinner1.setonitemselectedlistener(this); mbtnsave=(button)findviewbyid(r.id.button2); mbtnsave.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string str_spinner1 = spinner1.getselecteditem().tostring(); string str_spinner2 = spinner2.getselecteditem().tostring(); } }); //initializing textviews textviewname1 = (textview) findviewbyid(r.id.textviewname1); textviewname2 = (textview) findviewbyid(r.id.textviewname2); // textviewcourse = (textview) findviewbyid(r.id.textviewcourse); // textviewsession = (textview) findviewbyid(r.id.textviewsession); //this method fetch data url getdata1(); getdata2(); } @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { if(spinner1.getid()==r.id.spinner1) { str_spinner1 = spinner1.getselecteditem().tostring(); } else if(spinner2.getid()==r.id.spinner2) { str_spinner2 = spinner2.getselecteditem().tostring(); } /* switch (view.getid()){ case r.id.spinner1: getdata1(); break; case r.id.spinner2: getdata2(); break; }*/ } @override public void onnothingselected(adapterview<?> parent) { if (spinner1.getid() == r.id.spinner1) { //do textviewname1.settext(""); } else if (spinner2.getid() == r.id.spinner2) { //do textviewname2.settext(""); } } private void getdata1() { //creating string request stringrequest stringrequest1 = new stringrequest(config.data_url1, new response.listener<string>() { @override public void onresponse(string response1) { jsonobject j1 = null; try { //parsing fetched json string json object j1 = new jsonobject(response1); //storing array of json string our json array result1 = j1.getjsonarray(config.json_array1); //calling method getstudents students json array getstudents1(result1); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error1) { } }); //creating request queue requestqueue requestqueue1 = volley.newrequestqueue(this); //adding request queue requestqueue1.add(stringrequest1); } private void getstudents1(jsonarray j1) { //traversing through items in json array (int = 0; < j1.length(); i++) { try { //getting json object jsonobject json1 = j1.getjsonobject(i); //adding name of student array list students1.add(json1.getstring(config.tag_course)); } catch (jsonexception e) { e.printstacktrace(); } } //setting adapter show items in spinner spinner1.setadapter(new arrayadapter<string>(mainactivity_d3.this, android.r.layout.simple_spinner_dropdown_item, students1)); } //initializing textviews // textviewcourse = (textview) findviewbyid(r.id.textviewcourse); // textviewsession = (textview) findviewbyid(r.id.textviewsession); //this method fetch data url private void getdata2() { //creating string request stringrequest stringrequest2 = new stringrequest(config.data_url2, new response.listener<string>() { @override public void onresponse(string response2) { jsonobject j2 = null; try { //parsing fetched json string json object j2 = new jsonobject(response2); //storing array of json string our json array result = j2.getjsonarray(config.json_array); //calling method getstudents students json array getstudents2(result); } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error1) { } }); //creating request queue requestqueue requestqueue2 = volley.newrequestqueue(this); //adding request queue requestqueue2.add(stringrequest2); } private void getstudents2(jsonarray j2) { //traversing through items in json array (int = 0; < j2.length(); i++) { try { //getting json object jsonobject json2 = j2.getjsonobject(i); //adding name of student array list students2.add(json2.getstring(config.tag_username)); } catch (jsonexception e) { e.printstacktrace(); } } //setting adapter show items in spinner spinner2.setadapter(new arrayadapter<string>(mainactivity_d3.this, android.r.layout.simple_spinner_dropdown_item, students2)); } }
Comments
Post a Comment