searchablespinner
searchablespinner copied to clipboard
setSelectedItem don't work
Programmaticaly Selection doesn't work! searchableSpinner.setSelectedItem(index); searchableSpinner.setSelectedItem(item); Methods doesn't work.
if you used ArrayAdapter is wont work, you need to make a custom adapter which implements ISpinnerSelectedView
to make it work
@alaa7731 is right
you can try this code it worked perfectly for me
public class CustomArrayAdapter extends ArrayAdapter<String> implements ISpinnerSelectedView{
public List<String> items;
public Context context;
int resource;
public CustomArrayAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
super(context, resource, objects);
items = objects;
this.context = context;
this.resource =resource;
}
@Override
public View getNoSelectionView() {
return null;
}
@Override
public View getSelectedView(int position) {
View view = LayoutInflater.from(context).inflate(resource, null);
CheckedTextView textView = view.findViewById(android.R.id.text1);
textView.setText(items.get(position));
return view;
}
}
@saedkhaled Thanks, but I had to change getSelectedView(int position)
method to this, otherwise it wasn't selecting the right index on filtered data.
@Override
public View getSelectedView(int position) {
View view = LayoutInflater.from(context).inflate(resource, null);
CheckedTextView textView = view.findViewById(android.R.id.text1);
textView.setText(getItem(position).toString());
return view;
}