Android Spinner从数据库查询中选择项目

时间:2018-01-24 14:34:06

标签: php mysql android-studio

我有一个学生的课程数据库,我想通过studentID选择所有课程,但在android旋转器只显示表中的一个课程。我希望微调器显示学生ID选择的所有课程。

这是我的疑问:

public function getCourse($studID){
        $stmt = $this->con->prepare("SELECT * FROM courses WHERE studID = ?");
        $stmt -> bind_param("s", $studID);
        $stmt -> execute();
        return $stmt ->get_result()->fetch_assoc();
}

这是post方法

<?php


if($_SERVER['REQUEST_METHOD']=='GET'){
    $sid = $_GET['studID'];

require_once ('dboperations.php');

$db = new dboperations();

    $user = $db->getCourse($sid);
    $response = array();
    array_push($response,array(
    'coCode'=>$user['CourseCode'],
    'coName'=>$user['CourseName'],
    'coGroup'=>$user['CourseGroup'],
    'stID'=> $user['studID']
    ));



echo json_encode(array("result"=>$response));

mysqli_close($db);

}



?>

这是android studio中用于微调器的代码

private void getData(){
        String stid = SharedPrefManager.getInstance(this).getStudID();
        if (stid.equals("")) {
            Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
            //return;
        }
        loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
        String url = Constants.URL_GETCOURSE+stid;
        //Creating a string request
        StringRequest stringRequest = new StringRequest(Request.Method.GET,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        loading.dismiss();
                        JSONObject j;
                        try {
                            //Parsing the fetched Json String to JSON Object
                            j = new JSONObject(response);

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

                            //Calling method getStudents to get the students from the JSON Array
                            getStudents(result);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        //Creating a request queue
        RequestHandler.getInstance(this).addToRequestQueue(stringRequest);
    }

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

                //Adding the name of the student to array list
                students.add(json.getString(Constants.course_code));

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

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

    //Method to get student name of a particular position
    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(Constants.course_name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //Returning the name
        return name;
    }

    //Doing the same with this method as we did with getName()
    private String getCourse(int position){
        String course="";
        try {
            JSONObject json = result.getJSONObject(position);
            course = json.getString(Constants.course_group);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return course;
    }

    //Doing the same with this method as we did with getName()
    private String getSession(int position){
        //String sid = SharedPrefManager.getInstance(getApplicationContext()).getStudID();
        String session="";
        try {
            JSONObject json = result.getJSONObject(position);
            session = json.getString(Constants.stud_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return session;
    }

0 个答案:

没有答案