當我爲微調器選擇靜態數據時,其方法OnItemSelectedListener
工作正常,但是當我將JSON數據添加到微調器時,它會填充JSON數據及其數據OnItemSelectedListener
沒有被調用。可能是什麼問題呢? 這裏是我的代碼:當我將JSON數據設置爲微調器時,不會調用Spinner中的OnItemSelectedListener方法Android
class list_Activity extends Activity implements OnItemSelectedListener {
ProgressDialog progressDialog;
RequestQueue requestQueue;
String json_id;
String json_dept;
List<String> spinner_dept;
List<String> categories;
String selectedCity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading");
progressDialog.show();
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner3);
requestQueue= Volley.newRequestQueue(this);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
spinner_dept = new ArrayList<String>();
categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET,
"http://192.168.1.104/leave/public/api/department",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
progressDialog.dismiss();
try {
JSONArray jsonArray = response.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String b = jsonArray.getJSONObject(i).getString("department");
spinner_dept.add(b);
json_id = jsonObject.getString("identity");
json_dept = jsonObject.getString("department");
department deprt = new department();
deprt.setId(json_id);
deprt.setDepartment(json_dept);
// spinner_data.add(deprt);
// spinner_dept.add(jsonObject.getString("department"));
//textView.append(id+" "+ first_name + " " + last_name + "\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", "error from server response");
}
});
requestQueue.add(jsonObjectRequest);
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner_dept);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// selectedCity = spinner.getSelectedItem().toString();
if (position != 0) {
//put your actions here
String ch =spinner_dept.get(position);
Toast.makeText(parent.getContext(), "Selected: " + selectedCity, Toast.LENGTH_LONG).show();
} else {
// nothing here or toast
Toast.makeText(parent.getContext(), "Selected: " , Toast.LENGTH_LONG).show();
}
// Showing selected spinner item
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
的錯誤檢查日誌可能是一些你可以發現有 – Anil
參考https://stackoverflow.com/questions/2562248/how-to-keep-onitemselected-from-firing-off-on-a-newly - 實驗 - 微調 – sasikumar