问题
The following App flow shows what I expected ("Alfa" in the autocomplete list):
Open App -> click on button -> tap on autocomplete field -> type "al"
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:
<?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>
Thanks.
回答1:
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.
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
回答2:
I am pretty sure it is case sensitive. Try Al... that might be it. Also it could be because you are replacing names. I am not certain but when you pass through names it could be setting it to an instance var that won't update at the same time. so you will need to re-pass through the array.
来源:https://stackoverflow.com/questions/32210404/autocompletetextview-doesnt-update-arrayadapter-items