建筑
我有2个观看次数:Spinner
和AutoCompleteTextView
。
- AutoCompleteTextView
让Adapter
实施Filterable
并从我的API获取数据。
- 来自Spinner
的数据在设备上并插入AutoCompleteTextView
SpinnerAdapter
适配器
public class AutoCompleteCityAdapter extends BaseAdapter implements Filterable
public class CitySpinnerArrayAdapter extends ArrayAdapter implements SpinnerAdapter
问题
我已创建自定义PopulateListener
,以便将数据从AutoCompleteTextView
填充到Spinner
。
我将数据输入AutoCompleteTextView
,我得到结果,Spinner
通过我的PopulateListener
填充,直到重复整个操作。
当我删除文字并输入新文字时,当我在AdapterDataSetObserver was not registered
上拨打setAdapter()
时,我会收到Spinner
。
所有事情都在吹嘘的代码
autoCompleteCityAdapter.setPopulateListener(new AutoCompleteCityAdapter.OnPopulateListener() {
@Override
public void setCityData(ArrayList<String> cityData) {
if (cityData != null) {
CitySpinnerArrayAdapter spinnerArrayAdapter = new CitySpinnerArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, cityData);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_drop_down_item);
selectSpinner.setAdapter(spinnerArrayAdapter); //this throws the error
} else {
((CitySpinnerArrayAdapter)selectSpinner.getAdapter()).clear();
selectSpinner.setAdapter(null);
}
}
});
关于上面代码的两个字。
if - &gt;是CityData != null
因此有来自API的回复,因此我可以在Adapter
上设置Spinner
。
其他 - &gt;是因为CityData==null
因此API没有响应,这意味着删除或编辑了文本。需要清除Spinner
因为其数据在适当的时刻无效。
在AutoCompleteCityAdapter上设置的常规自定义侦听器
public interface OnPopulateListener {
public void setCityData(ArrayList<String> cityData);
}
public void setPopulateListener(OnPopulateListener populateListener) {
this.populateListener = populateListener;
}
错误信息
java.lang.IllegalStateException: Observer android.widget.AdapterView$AdapterDataSetObserver@53c1ce78 was not registered.
at android.database.Observable.unregisterObserver(Observable.java:69)
at android.widget.BaseAdapter.unregisterDataSetObserver(BaseAdapter.java:42)
at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:103)
at android.widget.Spinner.setAdapter(Spinner.java:371)
at com.test.calculator.ui.fragments.CalcFormFragment$3.setCityData(CalcFormFragment.java:285)
at com.test.calculator.ui.adapters.AutoCompleteCityAdapter$1.publishResults(AutoCompleteCityAdapter.java:120)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
=================== EDIT =====================
响应pskink的代码。
autoCompleteCityAdapter.setPopulateListener(new AutoCompleteCityAdapter.OnPopulateListener() {
@Override
public void setCityData(ArrayList<String> cityData) {
if (cityData != null) {
if (selectSpinner.getAdapter() != null) {
((CitySpinnerArrayAdapter)selectSpinner.getAdapter()).notifyDataSetChanged();
} else {
CitySpinnerArrayAdapter spinnerArrayAdapter = new CitySpinnerArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, cityData);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_drop_down_item);
selectSpinner.setAdapter(spinnerArrayAdapter);
}
} else {
((CitySpinnerArrayAdapter)selectSpinner.getAdapter()).notifyDataSetChanged();
}
}
});