在我的申请中,我正在与微调器合作。我想根据另一个微调器的项目选择得到一个微调器的值。我搜索了很多,但我发现只有自定义数据。但是我从服务中获得了这些微调值,所以任何人都可以帮我解决这个问题。
这是我的代码:
//Department spinner
Spinner spinner;
String URL="http://182.18.163.38/def.php?issue=1";
ArrayList<String> Department;
String uid;
//Deficiency category spinner
Spinner spinner2;
String URL2 = "http://182.18.163.38/def.php?issue=2&dept="+uid;
ArrayList<String>Deficiency;
String defid;
//Branch officer spinner
Spinner spinner3;
String URL3 = "http://182.18.163.38/def.php?issue=3";
ArrayList<String> BranchOfficer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_deficiency);
//spinner for department
Department = new ArrayList<>();
spinner=(Spinner)findViewById(R.id.select_department);
loadSpinnerData(URL);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String department = spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();
Toast.makeText(getApplicationContext(),department,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
//spinner for deficiency
Deficiency = new ArrayList<>();
spinner2 = (Spinner)findViewById(R.id.deficiency_category);
loadSpinnerDeficiency(URL2);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String deficiency = spinner2.getItemAtPosition(spinner2.getSelectedItemPosition()).toString();
Toast.makeText(getApplicationContext(),deficiency,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
}
private void loadSpinnerData(String url) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONArray jsonarray = new JSONArray(responseString);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String department = jsonobject.getString("Department");
uid = jsonobject.getString("ID");
Department.add(department);
}
spinner.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Department));
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void loadSpinnerDeficiency(String url2) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet("http://182.18.163.38/def.php?issue=2&dept="+uid);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONArray jsonarray = new JSONArray(responseString);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String defcat = jsonobject.getString("Deficiency");
defid = jsonobject.getString("ID");
Deficiency.add(defcat);
}
spinner2.setAdapter(new ArrayAdapter<String>(NewDeficiency.this, android.R.layout.simple_spinner_dropdown_item, Deficiency));
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
上面是我从网址获取值的代码。
答案 0 :(得分:0)
使用asynctask而不是在主线程中进行网络操作。
首先调用Web服务为第一个微调器加载数据,在asynctask的 onPostExecute()中填充数据。
在第一个微调器的 setOnItemSelectedListener 中,从第一个微调器(我相信你正在做)和c 所有asynctask的输入中加载第二个微调器 。您没有在第一个微调器的 setOnItemSelectedListener中加载第二个微调器。