如何在mysql db中保存微调器选定的项目?

时间:2016-07-20 06:54:35

标签: android mysql

在这里,我已经从mysql db中获取数据并将此数据显示到微调器中现在的问题是,我想将spinner选中的项目保存到mysql数据库中,使用不同的表,我从spinner中选择它应该保存到数据库的项目, 我怎样才能做到这一点?通过使用php mysql我将记录插入到微调器中,我想用spinner选择的项目更新寄存器表的记录。我可以使用微调器选择的项目值更新寄存器表吗?请建议我。

java文件

 public class MainActivity_d3 extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

        //Declaring an Spinner
        private Spinner spinner2, spinner1;
        private String str_spinner1, str_spinner2, s_name, s_course;
        //An ArrayList for Spinner Items

        private ArrayList<String> students1;
        private ArrayList<String> students2;

        Button mBtnSave;

        //JSON Array

        private JSONArray result1, result2, result;

        //TextViews to 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 the ArrayList
            students1 = new ArrayList<String>();
            students2 = new ArrayList<String>();

            //Initializing Spinner


            //Adding an Item Selected Listener to our Spinner
            //As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener


            spinner1 = (Spinner) findViewById(R.id.spinner1);
            spinner2 = (Spinner) findViewById(R.id.spinner2);

            spinner1.setOnItemSelectedListener(this);
            spinner2.setOnItemSelectedListener(this);

            mBtnSave = (Button) findViewById(R.id.button2);

            mBtnSave.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    submitForm();

                }
            });

            //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 will fetch the data from the URL
            getData1();
            getData2();

        }

        private void submitForm() {
            // Submit your form here. your form is valid
            //Toast.makeText(this, "Submitting form...", Toast.LENGTH_LONG).show();
            s_name = spinner1.getSelectedItem().toString();
            s_course = spinner2.getSelectedItem().toString();

             Toast.makeText(this, "Signing up...", Toast.LENGTH_SHORT).show();
            new InsertActivity(this).execute(s_name, s_course);


        }

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


        /*if(spinner1.getId()==R.id.spinner1) {
              textViewName1.setText(getName(position));
            }
            else if(spinner2.getId()==R.id.spinner2)
            {
                textViewName2.setText(getCourse(position));
            }
                  /*    switch (view.getId()){

                case R.id.spinner1:
                    getData1();
                    break;


                case R.id.spinner2:
                    getData2();
                    break;
            }*/
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {


               }


     /*   private String getName(int position){
            String name="";
            try {
                //Getting object of given index
                JSONObject json = result.getJSONObject(position);

                //Fetching name from that object
                name = json.getString(Config.TAG_NAME);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //Returning the name
            return name;
        }
        private String getCourse(int position){
            String course="";
            try {
                JSONObject json = result.getJSONObject(position);
                course = json.getString(Config.TAG_COURSE);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return course;
        }*/

        private void getData1() {
            //Creating a string request
            StringRequest stringRequest1 = new StringRequest(Config.DATA_URL1,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response1) {
                            JSONObject j1 = null;
                            try {
                                //Parsing the fetched Json String to JSON Object
                                j1 = new JSONObject(response1);

                                //Storing the Array of JSON String to our JSON Array
                                result1 = j1.getJSONArray(Config.JSON_ARRAY1);

                                //Calling method getStudents to get the students from the JSON Array
                                getStudents1(result1);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error1) {

                        }
                    });

            //Creating a request queue
            RequestQueue requestQueue1 = Volley.newRequestQueue(this);

            //Adding request to the queue
            requestQueue1.add(stringRequest1);
        }

        private void getStudents1(JSONArray j1) {
            //Traversing through all the items in the json array
            for (int i = 0; i < j1.length(); i++) {
                try {
                    //Getting json object
                    JSONObject json1 = j1.getJSONObject(i);

                    //Adding the name of the student to array list
                    students1.add(json1.getString(Config.TAG_COURSE));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            //Setting adapter to show the items in the 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 will fetch the data from the URL

        private void getData2() {
            //Creating a string request
            StringRequest stringRequest2 = new StringRequest(Config.DATA_URL2,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response2) {
                            JSONObject j2 = null;
                            try {
                                //Parsing the fetched Json String to JSON Object
                                j2 = new JSONObject(response2);

                                //Storing the Array of JSON String to our JSON Array
                                result = j2.getJSONArray(Config.JSON_ARRAY);

                                //Calling method getStudents to get the students from the JSON Array
                                getStudents2(result);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error1) {

                        }
                    });

            //Creating a request queue
            RequestQueue requestQueue2 = Volley.newRequestQueue(this);

            //Adding request to the queue
            requestQueue2.add(stringRequest2);
        }

        private void getStudents2(JSONArray j2) {
            //Traversing through all the items in the json array
            for (int i = 0; i < j2.length(); i++) {
                try {
                    //Getting json object
                    JSONObject json2 = j2.getJSONObject(i);

                    //Adding the name of the student to array list
                    students2.add(json2.getString(Config.TAG_USERNAME));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            //Setting adapter to show the items in the spinner
            spinner2.setAdapter(new ArrayAdapter<String>(MainActivity_d3.this, android.R.layout.simple_spinner_dropdown_item, students2));
        }
    }

    //InsertActivity

    public class InsertActivity extends AsyncTask<String, Void, String> {

        private Context context;
        Boolean error, success;

        public InsertActivity(Context context) {
            this.context = context;
        }

        protected void onPreExecute() {

        }

        @Override
        protected String doInBackground(String... arg0) {
            String s_name = arg0[0];
            //  String userName = arg0[1];
            String s_course = arg0[1];

            String link;
            String data;
            BufferedReader bufferedReader;
            String result;

            try {
                data = "?s_name=" + URLEncoder.encode(s_name, "UTF-8");
                //    data += "&username=" + URLEncoder.encode(userName, "UTF-8");
                data += "&s_course=" + URLEncoder.encode(s_course, "UTF-8");


                link = "http://mangoair.in/Spinner/insert_s1.php" + data;

                //   link = "http://hostogen.com/mangoair10/tryrr.php" + data;

                URL url = new URL(link);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();

                bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                result = bufferedReader.readLine();
                return result;

            } catch (Exception e) {
                // return new String("Exception: " + e.getMessage());
                // return null;
            }

            return null;
        }

      /*  @Override
        protected void onPostExecute(String result) {

            String jsonStr = result;
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                String query_result = jsonObj.getString("success");
                String message_result = jsonObj.getString("message");

                if (query_result.equalsIgnoreCase("1")) {
                    Toast.makeText(context,message_result , Toast.LENGTH_LONG).show();

                } else if (query_result.equalsIgnoreCase("-1")) {
                    Toast.makeText(context, message_result, Toast.LENGTH_LONG).show();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }*/

        @Override
        protected void onPostExecute(String result) {

            String jsonStr = result;

            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                String query_result = jsonObj.getString("query_result");

                if (query_result.equalsIgnoreCase("SUCCESS")) {
                    Toast.makeText(context, "Data inserted successfully. Signup successfully.", Toast.LENGTH_LONG).show();

                } else if (query_result.equalsIgnoreCase("FAILURE")) {
                    Toast.makeText(context, "Data could not be inserted, fill all records.", Toast.LENGTH_LONG).show();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    } 

1 个答案:

答案 0 :(得分:0)

从微调器中获取值作为字符串,并像普通字符串一样传递此字符串