Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用微调器将所选选项从givn 4-5选项插入数据库?_Android_Mysql - Fatal编程技术网

Android 如何使用微调器将所选选项从givn 4-5选项插入数据库?

Android 如何使用微调器将所选选项从givn 4-5选项插入数据库?,android,mysql,Android,Mysql,我正在开发一个应用程序,其中在某些页面上,用户需要从给定的4-5个选项中选择一个选项。目前,我正在使用微调器实现此功能。现在的问题是,如果用户选择第3个或第4个选项(仅允许单选项选择)选择如何在mysql php数据库中插入该特定选项 //MainActivity.java public class MainActivity_D extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

我正在开发一个应用程序,其中在某些页面上,用户需要从给定的4-5个选项中选择一个选项。目前,我正在使用微调器实现此功能。现在的问题是,如果用户选择第3个或第4个选项(仅允许单选项选择)选择如何在mysql php数据库中插入该特定选项

//MainActivity.java

    public class MainActivity_D 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_d);

                //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) {


                switch (parent.getId()){

                    case R.id.spinner1:
                        spinner1.setSelection(position);
                        s_name = (String) spinner1.getSelectedItem();
                        //    getData1();
                        // showToast("Spinner1: position=" + position);

                        break;


                    case R.id.spinner2:
                        spinner2.setSelection(position);
                        s_course = (String) spinner2.getSelectedItem();
                        //    getData2();
                        break;
                }

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

                    case R.id.spinner1:
                        //    getData1();
                        // showToast("Spinner1: position=" + position);

                        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_D.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_D.this, android.R.layout.simple_spinner_dropdown_item, students2));
            }


        }

        //InsertActivity.java

        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://insert_s1.php" + data;

                    //   link = "http://example.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");
                    Log.e("TAG", String.valueOf(query_result));

                    //   String message_result = jsonObj.getString("message");

                /*    if (query_result.equals("SUCCESS")) {
                        Toast.makeText(context, "Insert", Toast.LENGTH_LONG).show();

                    } else if(query_result.equals("FAILURE")){
                        Toast.makeText(context, "Unable to Insert", Toast.LENGTH_LONG).show();
                    }*/

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

            }
        }

        //Config.java

        public class Config {
            //JSON URL
         //   public static final String DATA_URL = "http://example.com/jsonphp1.php";
            public static final String DATA_URL1 = "http://example.com/jsonphp2.php";
            public static final String DATA_URL2 = "http://example.com/jsonphp1.php";

            //Tags used in the JSON String
            public static final String TAG_USERNAME = "username";
            public static final String TAG_NAME = "name";
            public static final String TAG_COURSE = "course";
            public static final String TAG_SESSION = "session";

            //JSON array name
            public static final String JSON_ARRAY = "result";
            public static final String JSON_ARRAY1 = "result1";
            public static final String JSON_ARRAY2 = "result2";


        }

//this is my php file
<?php
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); 

$s_name=$_POST['s_name'];
$s_course=$_POST['s_course'];
Sid=$_GET['id'];

  $query = "UPDATE `students` SET s_name = '$s_name', s_course= '$s_course' WHERE id='$id'"; 

 $inserted = mysqli_query($con, $query);

// echo  $inserted;

 if($inserted) {
    echo '{"query_result":"SUCCESS"}';
}
else{
    echo '{"query_result":"FAILURE"}';
}

mysqli_close($con);
?>
//MainActivity.java
公共类MainActivity_D扩展AppCompativeActivity实现AdapterView.OnItemSelectedListener{
//宣布成为纺纱工
专用喷丝头2、喷丝头1;
私人字符串STRU喷丝头1、STRU喷丝头2、s_名称、s_课程;
//微调器项的ArrayList
私立ArrayList学生1;
私立ArrayList学生2;
按钮mBtnSave;
//JSON数组
私有JSONArray结果1、结果2、结果;
//文本视图以显示详细信息
私有文本视图文本视图名称1;
私有文本视图文本视图名称2;
私人TextView TextView课程;
私有文本视图文本视图会话;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main活动);
//初始化ArrayList
students1=新数组列表();
students2=新数组列表();
//初始化微调器
//向微调器添加选定项侦听器
//由于我们已经将类Spinner.OnItemSelectedListener实现到这个类本身,我们将把它传递给setOnItemSelectedListener
喷丝头1=(喷丝头)findViewById(R.id.spinner1);
喷丝头2=(喷丝头)findViewById(R.id.spinner2);
喷丝头1.SetonimSelectedListener(此);
喷丝头2.SetonimSelectedListener(此);
mBtnSave=(按钮)findviewbyd(R.id.button2);
mBtnSave.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
submitForm();
}
});
//初始化文本视图
textViewName1=(TextView)findViewById(R.id.textViewName1);
textViewName2=(TextView)findViewById(R.id.textViewName2);
//textViewCourse=(TextView)findViewById(R.id.textViewCourse);
//textViewSession=(TextView)findViewById(R.id.textViewSession);
//此方法将从URL获取数据
getData1();
getData2();
}
私有void submitForm(){
//请在此提交您的表格。您的表格有效
//Toast.makeText(这是“提交表单…”,Toast.LENGTH_LONG.show();
//s_name=spinner1.getSelectedItem().toString();
//s_课程=喷丝头2.getSelectedItem().toString();
Toast.makeText(这是“注册…”,Toast.LENGTH_SHORT.show();
新插入活动(this).执行(s_名称,s_课程);
}
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
开关(parent.getId()){
案例R.id.喷丝头1:
喷丝头1.设置选择(位置);
s_name=(字符串)spinner1.getSelectedItem();
//getData1();
//showToast(“喷丝头1:position=“+position”);
打破
案例R.id.喷丝头2:
喷丝头2.选择(位置);
s_课程=(字符串)喷丝头2.getSelectedItem();
//getData2();
打破
}
/*if(spinner1.getId()==R.id.spinner1){
textViewName1.setText(getName(position));
}
else if(spinner2.getId()==R.id.spinner2)
{
textViewName2.setText(getCourse(position));
}
开关(parent.getId()){
案例R.id.喷丝头1:
//getData1();
//showToast(“喷丝头1:position=“+position”);
打破
案例R.id.喷丝头2:
//getData2();
打破
}*/
}
@凌驾
未选择公共无效(AdapterView父级){
}
/*私有字符串getName(int位置){
字符串名称=”;
试一试{
//获取给定索引的对象
JSONObject json=result.getJSONObject(位置);
//正在从该对象获取名称
name=json.getString(Config.TAG\u name);
}捕获(JSONException e){
e、 printStackTrace();
}
//返回名称
返回名称;
}
私有字符串getCourse(int位置){
字符串course=“”;
试一试{
JSONObject json=result.getJSONObject(位置);
course=json.getString(Config.TAG\u course);
}捕获(JSONException e){
e、 printStackTrace();
}
返回路线;
}*/
私有void getData1(){
//创建字符串请求
StringRequest stringRequest1=新的StringRequest(Config.DATA_URL1,
新的Response.Listener(){
@凌驾
@Override
        protected void onPostExecute(String result) {

            String jsonStr = result;

            Log.e("TAG",jsonStr );
        }