AutoCompleteTextView不更新arrayadapter项目 [英] AutoCompleteTextView doesn't update arrayadapter items
问题描述
下面的程序流程显示了我的预期(阿尔法中的自动完成列表):
The following App flow shows what I expected ("Alfa" in the autocomplete list):
打开应用程序 - >点击按钮 - >自动完成对自来水领域 - >键入人
Open App -> click on button -> tap on autocomplete field -> type "al"
但是这一次失败(如果I型BR,布拉沃仍然是在列表中):
But this one fails (and if I type "br", "Bravo" is still in the list):
打开应用程序 - >水龙头上自动完成场 - >输入任何东西,然后将其删除 - >点击按钮 - >自动完成对自来水领域 - >键入人
Open App -> tap on autocomplete field -> type anything and then delete it -> click on button -> tap on autocomplete field -> type "al"
为什么没有在第二顺序更新列表?
Why the list is not updated in the second sequence?
public class DisplayFragment extends Fragment {
AutoCompleteTextView autoCTVShop;
Button buttonSend;
String[] names= {"Bravo","Charlie","Delta","Foxtrot"};
ArrayAdapter<String> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_display, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
autoCTVShop = (AutoCompleteTextView) getActivity().findViewById(R.id.autoCompleteTextViewShop);
adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, names);
autoCTVShop.setAdapter(adapter);
buttonSend = (Button) getActivity().findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//The following two lines don't work as I expected
names[0]="Alfa";
adapter.notifyDataSetChanged();
}
});
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
}
fragment_display.xml:
fragment_display.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/autoCompleteTextViewShop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:imeOptions="actionNext"
android:singleLine="true" />
<Button
android:id="@+id/buttonSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:enabled="true"
android:text="Button" />
</LinearLayout>
感谢。
推荐答案
在内部, ArrayAdapter
使用2列出来存储和处理的项目。开始时,您的项目中通过存储,并通过一个名为列表处理 mObjects
。任何更改您的外名称
对象将反映在 mObjects
列表也是如此。
Internally the ArrayAdapter
uses 2 lists to store and handle the items. Initially your passed in items are stored and handled through a list named mObjects
. Any changes to your outside names
object would be reflected in the mObjects
list as well.
一旦你开始过滤,第二个名单,名为 mOriginalValues
创建。在过滤流量, mObjects
将被用作当前值的容器,而 mOriginalValues
作为复位并重新过滤器容器。更改名称
值将没有影响了。您可以直接更改适配器值(adapter.insert(...),adapter.remove(...) - 它会修改 mOriginalValues
)或重新创建适配器
As soon as you start filtering, a second list, named mOriginalValues
is created. During the filtering flow, mObjects
is going to be used as the "current values" container, while mOriginalValues
is used as a "reset to and re-filter" container. Changing the names
values will have no influence anymore. You could change the adapter values directly (adapter.insert(...), adapter.remove(...) - it will modify mOriginalValues
) or recreate the adapter
这篇关于AutoCompleteTextView不更新arrayadapter项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!