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).
@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);
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With