Android 如何动态更新微调器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3283337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:28:35  来源:igfitidea点击:

How to update a spinner dynamically?

androidandroid-spinner

提问by 2Real

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

我一直在尝试在 android 中动态更新我的微调器,但我尝试的一切都没有奏效。

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

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

typeList 的值是正确的,但它们没有在 Spinner 中更新。

回答by AdamC

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.

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

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

回答by Fifer Sheep

You can't directly modify the original Listthen call notifyDataSetChanged()like on other adapters, as it doesn't hold on to the original List.

您不能像在其他适配器上一样直接修改原始文件List然后调用notifyDataSetChanged(),因为它不保留原始List.

You can, however, achieve the same result using the adapter itself, like so:

但是,您可以使用适配器本身实现相同的结果,如下所示:

spinnerAdapter.clear();
spinnerAdapter.addAll(updatedListData);
spinnerAdapter.notifyDataSetChanged(); // optional, as the dataset change should trigger this by default

Based on thisanswer from user392117.

基于来自 user392117 的这个回答。

edit:By default, methods that change the list like add()and remove()automatically call notifyDataSetChanged()(see Android Developer Documentation for setNotifyOnChange(boolean))

编辑:默认情况下,更改列表等的方法add(),并remove()自动调用notifyDataSetChanged()(见的setNotifyOnChange Android开发者文档(布尔)

public void setNotifyOnChange (boolean notifyOnChange)

Control whether methods that change the list (add, addAll(java.util.Collection), addAll(java.lang.Object[]), insert, remove, clear, sort(java.util.Comparator)) automatically call notifyDataSetChanged. If set to false, caller must manually call notifyDataSetChanged() to have the changes reflected in the attached view. The default is true, and calling notifyDataSetChanged() resets the flag to true.

public void setNotifyOnChange (boolean notifyOnChange)

控制更改列表的方法(add、addAll(java.util.Collection)、addAll(java.lang.Object[])、insert、remove、clear、sort(java.util.Comparator))是否自动调用notifyDataSetChanged。如果设置为 false,调用者必须手动调用 notifyDataSetChanged() 以在附加视图中反映更改。默认值为 true,调用 notifyDataSetChanged() 会将标志重置为 true。

So you should not need to call notifyDataSetChanged()every time. If you find that this is the case, you can use setNotifyOnChange(true)

所以你不需要notifyDataSetChanged()每次都打电话。如果你发现是这种情况,你可以使用 setNotifyOnChange(true)

spinnerAdapter.setNotifyOnChange(true); //only need to call this once
spinnerAdapter.add(Object); //no need to call notifyDataSetChanged()

回答by Robby Pond

You only need to call setAdapter() once and you call adapter.notifyDataSetChanged()to update the data.

您只需要调用 setAdapter() 一次,然后调用adapter.notifyDataSetChanged()来更新数据。

回答by Dharmendra

When you have data changed in your list and when you want to update the spinner then

Create a new object of the adapter and set that adapter to the spinner. It will work sure.

Best of luck.

当您在列表中更改数据并且想要更新微调器时

创建适配器的新对象并将该适配器设置为微调器。它肯定会起作用。

祝你好运。

Edit: Also you need to call notifyDataSetChanged() on the adapter.

编辑:您还需要在适配器上调用 notifyDataSetChanged()。

回答by Cristian

Is there a typo or something? Which is the difference between dbAdapterand adapter. If the Spinner has already the adapter, you don't have to re-assign it. More over, the only think you have to do is update the adapter and call notifyDataSetChangedmethod.

有错别字之类的吗?这之间的差别dbAdapteradapter。如果 Spinner 已有适配器,则不必重新分配它。此外,您唯一需要做的就是更新适配器和调用notifyDataSetChanged方法。

typeList = adapter.getList(); //array list with the values
// change the values, and then
adapter.notifyDataSetChanged();

回答by no-name

Using add/remove on your adapter and using notifyDataSetChanged() enables you not having to create new adapters over and over again.

在适配器上使用添加/删除并使用 notifyDataSetChanged() 使您不必一遍又一遍地创建新适配器。

Declare your adapter global

将您的适配器声明为全局

ArrayAdapter<Object> adapter;

When you add something to the List of Objects the adapter is attached to (Strings or whatever object you use) add an add function to the adapter and call notifyDataSetChanged:

当您向适配器所连接的对象列表(字符串或您使用的任何对象)添加一些内容时,向适配器添加添加函数并调用 notifyDataSetChanged:

adaper.add(Object);
adapter.notifyDataSetChanged();

and when you remove an item from the List add also:

当您从列表中删除项目时,还添加:

adapter.remove(Object);
adapter.notifyDataSetChanged();

edit:By default, methods that change the list like add() and remove() automatically call notifyDataSetChanged() (see Android Developer Documentation for setNotifyOnChange(boolean))

编辑:默认情况下,更改列表的方法如 add() 和 remove() 会自动调用 notifyDataSetChanged() (请参阅Android 开发人员文档中的 setNotifyOnChange(boolean)

public void setNotifyOnChange (boolean notifyOnChange)

Control whether methods that change the list (add, addAll(java.util.Collection), addAll(java.lang.Object[]), insert, remove, clear, sort(java.util.Comparator)) automatically call notifyDataSetChanged. If set to false, caller must manually call notifyDataSetChanged() to have the changes reflected in the attached view. The default is true, and calling notifyDataSetChanged() resets the flag to true.

public void setNotifyOnChange (boolean notifyOnChange)

控制更改列表的方法(add、addAll(java.util.Collection)、addAll(java.lang.Object[])、insert、remove、clear、sort(java.util.Comparator))是否自动调用notifyDataSetChanged。如果设置为 false,调用者必须手动调用 notifyDataSetChanged() 以在附加视图中反映更改。默认值为 true,调用 notifyDataSetChanged() 会将标志重置为 true。

So you should not need to call notifyDataSetChanged() every time. If you find that this is the case you you can use setNotifyOnChange(true)

所以你不需要每次都调用 notifyDataSetChanged() 。如果您发现是这种情况,您可以使用 setNotifyOnChange(true)

adapter.setNotifyOnChange(true); //only need to call this once
adapter.add(Object); //no need to call notifyDataSetChanged()

回答by Kapil D

Change the underlying data and call the notifyDataSetChanged() on adapter.

更改底层数据并在适配器上调用 notifyDataSetChanged()。

   list.clear();
  list.add("A");
      list.add("B");
  dataAdapter.notifyDataSetChanged();

回答by codingForFun

After you change your data, you will need to add the following code:

更改数据后,您需要添加以下代码:

typeList = dbAdapter.getList()              
adapter = new ArrayAdapter<String>(v.getContext(),
    android.R.layout.simple_spinner_dropdown_item,typeList);            
groupSpinner.setAdapter(adapter);

回答by kgao

Apparently after you do typeList = dbAdapter.getList(), the variable typeListpoints to a different List rather than the one you fed to the adapter originally and the adapter would be somewhat confused.

显然,在您执行之后typeList = dbAdapter.getList(),该变量typeList指向一个不同的列表,而不是您最初提供给适配器的列表,并且适配器会有些混乱。

So you should use the following code:

所以你应该使用以下代码:

typeList.clear();
typeList.addAll(dbAdapter.getList());

回答by toshkinl

When you set up the spinner adapter add

设置微调适配器时添加

spinnerAdapter.setNotifyOnChange(true);

From then on when you add new data it will be automatically updated.

从那时起,当您添加新数据时,它将自动更新。