searchablespinner icon indicating copy to clipboard operation
searchablespinner copied to clipboard

setSelectedItem don't work

Open udvsharp opened this issue 5 years ago • 3 comments

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

udvsharp avatar May 04 '19 13:05 udvsharp

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

alaa7731 avatar Oct 20 '20 12:10 alaa7731

@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 avatar Dec 24 '20 20:12 saedkhaled

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

naveed-iqbal avatar Nov 30 '21 03:11 naveed-iqbal