android为什么点击spinner用静态数据可以监听,而动态加载的不行?
``` Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case -1:
Toast.makeText(Cache.getContext().getApplicationContext(), getResources().getString(R.string.errorMsg), Toast.LENGTH_SHORT).show();
break;
case 2://公司名称下拉表
if (companyNameDto != null && companyNameDto.getStatus() != null) {
if (companyNameDto.getStatus() == 200) {
List<MonitorCompany> list = companyNameDto.getData();
Log.i("sysout", "companyNameList:" + list);
companyNames.add("所有单位");
for (int i = 0; i < list.size(); i++) {
companyNames.add(list.get(i).getName());
}
Log.i("sysout", "companyNames:" + list.size() + companyNames);
}
}
}
break;
default:
break;
}
}
};
/**
-
加载公司列表
*/
public void loadCompany() {
MyApplication.ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
@Override
public void run() {
try {
companyNameDto = ServerMain.listCompanyMenu("get", getResources().getString(R.string.listCompanyMenu), null);
Log.i("Sysout", "companyData:" + companyNameDto);
handler.sendEmptyMessage(2);
} catch (Exception e) {
handler.sendEmptyMessage(-1);
e.printStackTrace();
}
}});
List array1 = Arrays.asList("所有公司", "xx科技", "xx大学", "xx市", "上海市");
List array2 = companyNames;
Log.i("sysout", "array1:" +array1+ companyNames);
DevListSpinAdapter adapterOne = new DevListSpinAdapter(this, array2, R.layout.spinner_item);
spinner1.setAdapter(adapterOne);
// spinner1.setSelection(0, true);
// spinner1.setSelection(0);
}/**
-
Spinner1点击事件
*/
public class Spinner1ClickListener implements AdapterView.OnItemSelectedListener {
// String ListenName = spinner1.getSelectedItem().toString();@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
//String str = (String) adapterView.getItemAtPosition(position);
String str=spinner1.getSelectedItem().toString();
//String str = (String) array1[position];
// String str = (String) adapterView.getAdapter().getItem(i);
/* String str = (String) companyNames.get(i);
String str1 = (String) companyNames.toArray()[i];*/
companyName = str;
Log.i("sysout", "Spinner1str:" + str + companyName + companyNames);Toast.makeText(DeviceListActivity.this, str+companyName, Toast.LENGTH_SHORT).show();
}@Override
public void onNothingSelected(AdapterView<?> adapterView) {}
}