Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my custom spinner filling with my package name?

I found this workaround for adding a hint to a dropdown spinner. It looked promising, but instead of getting my desired strings to show in the spinner, I'm getting something that looks like my package name (can't see the full name).
enter image description here

@IsaacCisneros created new SpinnerItem and MySpinnerAdapter classes. I implemented his new classes exactly. My implementation looks like this:

ArrayList<SpinnerItem> credValues = new ArrayList<SpinnerItem>();
         credValues.add(new SpinnerItem("3.0",false));
         credValues.add(new SpinnerItem("Credit", true));

         MySpinnerAdapter adapter_cred = new MySpinnerAdapter(this.getActivity(),
                 android.R.layout.simple_spinner_item, credValues);
         adapter_cred.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

         Spinner spinCred = (Spinner)rootView.findViewById(R.id.cred_spinner);
         spinCred.setAdapter(adapter_cred);
         spinCred.setSelection(credValues.size() - 1);

And his looks like this:

ArrayList<SpinnerItem> items = new ArrayList<SpinnerItem>();
    items.add(new SpinnerItem("Item 1", false));
    items.add(new SpinnerItem("Item 2", false));
    items.add(new SpinnerItem("HINT", true)); // Last item 

    MySpinnerAdapter adapter = new MySpinnerAdapter(this, android.R.layout.simple_spinner_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setSelection(items.size() - 1);

So why is mine acting weird? The context for my code is the onCreateView of my AddItemFragment within my activity. What am I referencing wrong? Thanks!


EDIT: The adapter class code was requested...

class SpinnerItem {
    private final String text;
    private final boolean isHint;

    public SpinnerItem(String strItem, boolean flag) {
        this.isHint = flag;
        this.text = strItem;
    }

    public String getItemString() {
        return text;
    }

    public boolean isHint() {
        return isHint;
    }
}

class MySpinnerAdapter extends ArrayAdapter<SpinnerItem> {
    public MySpinnerAdapter(Context context, int resource, List<SpinnerItem> objects) {
        super(context, resource, objects);
    }

    @Override
    public int getCount() {
        return super.getCount() - 1; // This makes the trick: do not show last item
    }

    @Override
    public SpinnerItem getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }
}
like image 921
NSouth Avatar asked Apr 03 '14 02:04

NSouth


2 Answers

The reason is because you have not overridden the getView() method of the adapter. Because of this, it uses the default implementation which looks like this:

T item = getItem(position);

if (item instanceof CharSequence) {
    text.setText((CharSequence)item);
} 
else {
    text.setText(item.toString());
}

In your SpinnerItem you do not have a toString() method, so it uses the default value (which is what you see as the package name)

One way to fix this would be by adding a toString() method to your SpinnerItem:

class SpinnerItem {
    private final String text;
    private final boolean isHint;

    public SpinnerItem(String strItem, boolean flag) {
        this.isHint = flag;
        this.text = strItem;
    }

    public String getItemString() {
        return text;
    }

    public boolean isHint() {
        return isHint;
    }

    @Override
    public String toString() {
        return text;
    }
}

If you want more control, such as different color for the hint text etc. I recommend you implement the getView() method on your own.

like image 150
Amulya Khare Avatar answered Oct 13 '22 00:10

Amulya Khare


Probably your adapter extends android.widget.ArrayAdapter. By default in implementation of ArrayAdapter - in android source code - as Amulya Khare said, the getView method, gains value of text view through

T item = getItem(position);
if (item instanceof CharSequence) {
   text.setText((CharSequence)item);
} 
else {
   text.setText(item.toString());
}

By default each class that inherits from Object class - like your SpinneItem class - returns package name through toString method. so you can override toString of SpinnerItem or override getView of MySpinnerAdapter.

like image 29
Abolhassan Abdolalizade Avatar answered Oct 13 '22 01:10

Abolhassan Abdolalizade