根据 mysql 数据库 (Android) 中的其他微调器更改微调器值
changing spinners value based on other spinners from mysql database (Android)
谁能给我指点一下这方面的任何教程或指南。如何根据前一个微调器的选择更新一个微调器的值。数据值来自 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("");
}
}
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();
});
希望这会奏效。
首先,您根据 Spinners 的其他三个值填充了 Spinner。您可以根据其他 Spinner 的值执行查询。
填充第四个Spinner后,可以使用Spinner的setSelection(index)
方法设置值。
谁能给我指点一下这方面的任何教程或指南。如何根据前一个微调器的选择更新一个微调器的值。数据值来自 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("");
}
}
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();
});
希望这会奏效。
首先,您根据 Spinners 的其他三个值填充了 Spinner。您可以根据其他 Spinner 的值执行查询。
填充第四个Spinner后,可以使用Spinner的setSelection(index)
方法设置值。