超链码语者 2020-03-20 16:43 采纳率: 0%
浏览 386

android为什么点击spinner用静态数据可以监听,而动态加载的不行?

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) {

    }
    }

  • 写回答

1条回答 默认 最新

  • 柠檬味水溶cc 2020-03-23 14:44
    关注

    我也遇到了同样的情况,添加了 spinner2Adapter.notifyDataSetChanged();后问题解决了

    评论

报告相同问题?

悬赏问题

  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!