如何动态更新微调器? [英] How to update a spinner dynamically?

查看:77
本文介绍了如何动态更新微调器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试动态更新android中的微调器,但没有尝试成功.

I've been trying to update my spinner in android dynamically but nothing I try has been working.

这是我用来更新微调器的以下代码.

This is the following code I'm using to update the spinner.

typeList = dbAdapter.getList(); //array list with the values

adapter.notifyDataSetChanged();
groupSpinner.postInvalidate();
groupSpinner.setAdapter(adapter);

typeList的值正确,但是在Spinner中没有更新.

The values of typeList are correct but they're not being updated in the Spinner.

推荐答案

实际上,您要么必须在适配器上调用clear/add,要么创建并设置新的适配器.适配器不保留对列表的引用(它只是在构造时在列表上调用toArray),因此无法对其进行更新.

Actually, you either have to call clear/add on the adapter, or create and set a new adapter. The adapter does not retain a reference to your list (it is only calling toArray on your list at construction), so there is no way for it to update itself.

dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, newStringList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategory.setAdapter(dataAdapter);

这篇关于如何动态更新微调器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆