我有两个微调器“ Route”和“ Station”,第二个微调器依赖于第一个微调器,因此我开发了代码,该代码可以正常工作,但是现在我需要获取第二个微调器的选定值(Station )并将其发送到另一个网址服务器(php)// http://localhost/Android/v1/test1.php 但是我没有找到解决方案。
我尝试使用routeSpinner进行操作,但是我没有得到所选的值,所以它不起作用。
spinnerStation = (Spinner)findViewById(R.id.spinnerStation);
spinnerRoute = (Spinner)findViewById(R.id.spinnerRoute);
stationList = new ArrayList<Station>();
spinnerRoute.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Refresh Spinner
stationList.clear();
new GetStationFromServer().execute();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
private class GetStationFromServer extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ProfileActivity.this);
pDialog.setMessage("Fetching Data");
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
routeName = spinnerRoute.getSelectedItem().toString();
Handler jsonParser = new Handler();
String json = jsonParser.makeServiceCall("http://192.168.1.2/ Android/v1/get_station.php?route="+routeName, Handler.GET);
Log.e("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray station = jsonObj
.getJSONArray("station");
for (int i = 0; i < station.length(); i++) {
JSONObject modObj =(JSONObject)station.get(i);
Station mod = new Station ( modObj.getString("station"));
stationList.add(mod);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
populateSpinnerStation();
}
}
private void populateSpinnerStation() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < stationList.size(); i++) {
lables.add(stationList.get(i).getStation());
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerStation.setAdapter(spinnerAdapter);
}
我希望像将路由微调器一样将选定的站点微调器传递给服务器,然后继续我的应用程序,但是它显示错误“未定义索引”
答案 0 :(得分:0)
您是否尝试过将字符串显式传递给AsyncTask,而不是要求Async任务查找选定的字符串?
在路由微调器侦听器中类似:
spinnerRoute.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Refresh Spinner
stationList.clear();
routeName = spinnerRoute.getSelectedItem().toString();
new GetStationFromServer().execute(routeName);
}
并将异步任务更改为:
private class GetStationFromServer extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
String s = params[0];
Handler jsonParser = new Handler();
String json = jsonParser.makeServiceCall("http://192.168.1.2/
Android/v1/get_station.php?route="+s, Handler.GET);
}