michaelprimez / searchablespinner

Searchable Spinner
Apache License 2.0
314 stars 102 forks source link

setSelectedItem don't work #49

Open udvsharp opened 5 years ago

udvsharp commented 5 years ago

Programmaticaly Selection doesn't work! searchableSpinner.setSelectedItem(index); searchableSpinner.setSelectedItem(item); Methods doesn't work.

alaa7731 commented 3 years ago

if you used ArrayAdapter is wont work, you need to make a custom adapter which implements ISpinnerSelectedView to make it work

saedkhaled commented 3 years ago

@alaa7731 is right

you can try this code it worked perfectly for me

public class CustomArrayAdapter extends ArrayAdapter 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;
}

}

naveed-iqbal commented 2 years ago

@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;
    }