设置Adapter给SearchableSpinner后数据数量异常变为1的问题
I’ve run into similar quirks with third-party SearchableSpinner controls before, so let’s break down what’s happening and how to fix it:
First, let’s confirm the basics: your provinceAdapter has 34 items before attaching to the spinner, so your data source (arrayProvince) and Adapter setup are solid. The problem lies in how the SearchableSpinner is handling the Adapter you pass in.
Common Causes & Solutions
1. Test with a Native Spinner First
Rule out issues with your data or Adapter logic by swapping in a native Android Spinner temporarily:
// Add a native Spinner to your layout for testing Spinner testSpinner = getView().findViewById(R.id.test_spinner); ArrayAdapter<String> testAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, arrayProvince); testAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); testSpinner.setAdapter(testAdapter); System.out.println("Native Spinner count: " + testSpinner.getAdapter().getCount());
If this prints 34, the problem is definitely with the SearchableSpinner control itself, not your data.
2. Check for Layout Property Conflicts
Open your layout XML where the SearchableSpinner is defined. If you’ve set the android:entries attribute, this will override any Adapter you set programmatically. Remove that attribute if present:
<!-- Bad: entries attribute conflicts with programmatic Adapter --> <com.toptoche.searchablespinnerlibrary.SearchableSpinner android:id="@+id/spinner_province" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/some_provinces" /> <!-- Good: no conflicting entries attribute --> <com.toptoche.searchablespinnerlibrary.SearchableSpinner android:id="@+id/spinner_province" android:layout_width="match_parent" android:layout_height="wrap_content" />
3. Explicitly Define Adapter Generic Type
Your current code uses inferred generics for the ArrayAdapter, which can sometimes cause type mismatch issues with third-party controls. Explicitly specify the type and reset the Adapter cleanly:
// Explicitly define String generic type ArrayAdapter<String> provinceAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, arrayProvince); provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Clear any existing Adapter first before setting the new one spinnerProvince.setAdapter(null); spinnerProvince.setAdapter(provinceAdapter);
4. Check for Library Bugs
If you’re using an older version of the SearchableSpinner library (like the popular toptoche implementation), some versions have bugs where the Adapter gets incorrectly wrapped or truncated. Try updating to the latest version, or switch to an alternative like MaterialSearchSpinner if the issue persists.
5. Verify No Subsequent Code Modifies the Adapter
Double-check your code after the setAdapter call—make sure there’s no other logic that re-sets the Adapter, clears the arrayProvince list, or modifies the Spinner’s data. Since arrayProvince is marked final, it can’t be reinitialized, but ensure no other code calls clear() on it (though your log proves the Adapter has 34 items before setting, so this is less likely).
Final Troubleshooting Step
If none of the above works, override the getCount() method of your Adapter temporarily to see if the SearchableSpinner is interfering with it:
ArrayAdapter<String> provinceAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, arrayProvince) { @Override public int getCount() { int count = super.getCount(); System.out.println("Adapter getCount called, returning: " + count); return count; } };
This will show you if the SearchableSpinner is calling getCount() and getting the correct value, or if it’s somehow replacing your Adapter entirely.
内容的提问来源于stack exchange,提问作者megifernanda

