Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner OnItemSelected With Custom Adapter

I have a Spinner that uses a custom adapter where the getView() is overridden. I'm having trouble capturing the OnItemSelected event, which I believe is related to the custom adapter. In my onCreate(), I have this:

superGroupAdapter = new SuperGroupAdapter(context, R.layout.row_sg, sg_list);
sgSpinner.setAdapter(superGroupAdapter);

sgSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
        Log.d(Constants.TAG, "sg spinner on item selected");
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
});

And this is my custom adapter class:

public class SuperGroupAdapter extends ArrayAdapter<String> {

    @Inject SharedVisualElements sharedVisualElements;

    Context context;
    ArrayList<String> sg_list;

    public SuperGroupAdapter(Context context, int textViewResourceId, ArrayList<String> sg_list) {
        super(context, textViewResourceId, sg_list);

        // add this line for any class that want to use any of the singleton objects
        Injector.INSTANCE.getAppComponent().inject(this);

        this.context = context;
        this.sg_list = sg_list;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

        parent.setBackgroundColor(sharedVisualElements.backgroundColor());

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.row_sg, parent, false);

        TextView label = (TextView) row.findViewById(R.id.sg_name);
        label.setText(sg_list.get(position));
        label.setTypeface(sharedVisualElements.font());
        label.setTextColor(sharedVisualElements.primaryFontColor());
        label.setGravity(Gravity.CENTER_HORIZONTAL);

        return row;
    }
}

When the activity initializes, I see the log output

sg spinner on item selected

But that is the last time I see it. No matter how many times I select an item from the spinner, it never fires again. I've looked all over for a way to trap this, but to no avail. Can anyone help? Thank you.

EDIT I've also tried changing the class signature to implement OnItemSelected and declaring the listener as a separate method as is explained in the Android docs, but got the same result.

I'm seriously at a loss on this one. I appreciate any help.

like image 947
AndroidDev Avatar asked Oct 18 '25 10:10

AndroidDev


2 Answers

Well, I figured this out. After reviewing some other posts, it occurred to me that in my test data, I only had one item in my spinner list. The OnItemSelectedListener only fires when you change the selection.

From the Android docs for OnItemSelectedListener

This callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item.

So, when the activity initialized, it selected the item at position 0. When I tapped the spinner and 'selected' the same item, this action does not fire that event. Live and learn.

like image 107
AndroidDev Avatar answered Oct 20 '25 23:10

AndroidDev


I think you are missing this before the adapter SuperGroupAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

like image 34
Benjamin Fell Avatar answered Oct 20 '25 23:10

Benjamin Fell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!