基于mysql数据库(Android)中的其他微调器更改微调器值

时间:2016-01-28 09:29:43

标签: android mysql json android-spinner

有人可以指点我这方面的任何教程或指南。如何根据前一个微调器的选择更新一个微调器的值。数据值来自mysql数据库。我搜索过但没有找到任何满意的答案。请帮助。

spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(spinnerListener);
    spinner2 = (Spinner) findViewById(R.id.spinnersportcentername);
    spinner2.setOnItemSelectedListener(spinnerListener);

从上面我有2个微调器。现在我从数据库中获取数据。代码如下。我正在获取数据。现在我需要根据第一个微调器中选择的项更新第二个微调器的值。我怎么做?我现在几乎无能为力。

private void getData(){
    //Creating a string request
    StringRequest stringRequest = new StringRequest(FacilityConfig.DATA_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    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(FacilityConfig.JSON_ARRAY);

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

                }
            });

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

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

private void getFacility(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);

                            facilities.add(json.getString  (FacilityConfig.TAG_FACILITY));

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

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

}


private void getData2(){
    //Creating a string request
    StringRequest stringRequest = new StringRequest(SPnameConfig.DATA_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    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(SPnameConfig.JSON_ARRAY2);


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

                }
            });

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

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

private void getSpname(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);


            sportcenternames.add(json.getString(SPnameConfig.TAG_SPORTCENTERNAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //Setting adapter to show the items in the spinner

    spinner2.setAdapter(new ArrayAdapter<String>(FragmentOne.this, android.R.layout.simple_spinner_dropdown_item, sportcenternames));
}



public class myOnItemSelectedListener implements AdapterView.OnItemSelectedListener {

    Context mContext;

    public myOnItemSelectedListener(Context context){
        this.mContext = context;
    }

    private String getprice(int pos){
        String price="";
        try {
            //Getting object of given index
            JSONObject json = result.getJSONObject(pos);

            //Fetching name from that object
            price = json.getString(FacilityConfig.TAG_PRICE);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //Returning the name
        return price;
    }
    public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) {

        switch (parent.getId()) {
            case R.id.spinner:
                Toast.makeText(parent.getContext(),
                        "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
                        Toast.LENGTH_SHORT).show();
                textViewPrice.setText(getprice(pos));
                break;
            case R.id.spinnersportcentername:
                Toast.makeText(parent.getContext(),
                        "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
                        Toast.LENGTH_SHORT).show();
                break;
        }


    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        textViewPrice.setText("");
    }
}

2 个答案:

答案 0 :(得分:1)

    firstSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    public void onItemSelected(AdapterView<?> parent, View view, int position,       long id) {
    ArrayAdapter<String> newAdaptor = new ArrayAdapter<String>(this,  android.R.layout
   .simple_spinner_dropdown_item, value);
    secondSpinner.setAdapter(adapter);

}
   @Override
   public void onNothingSelected(AdapterView<?> parent) {
    Toast.makeText(getApplicationContext(),"Please seelct your  country",Toast.LENGTH_SHORT).show();
});
希望这会奏效。

答案 1 :(得分:0)

首先,您已根据Spinners的其他三个值填充Spinner。您可以根据其他Spinners的值执行查询。

填充第四个Spinner后,您可以使用Spinner的setSelection(index)方法设置该值。