create adapter to fill Spinner with objects

26,116

Solution 1

Here is my 5 cents. I had a similar problem. I was working with SimpleCursorAdapter which implements interface SpinnerAdapter, but arrived only until SDK version 11 (Android 3.0). I intended my app to work with SDK 8 (Android 2.2) and up, so I had to replace SimpleCursorAdapter with another, or my own. The real challenger was that I also used a custom XML layout for spinner and in it showed several fields from cursor i.e. cursor adapter. So here is my solution after a lot of research, and the info wasn't easy to come by.

Here is the layout file used in spinner named spin_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" >
<TextView 
    android:id="@+id/field1"
    android:textColor="#000"
    android:gravity="center"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:textSize="24sp" />
<TextView 
    android:id="@+id/field2"
    android:textColor="#000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textSize="24sp" />
</LinearLayout>

And here is the adapter implementing SpinnerAdapter and extending (using as a little helper) BaseAdapter. The Cursor that was used originally was transformed into List and passed in constructor, together with activity containing the spinner.

public class MyCursorAdapter extends BaseAdapter implements SpinnerAdapter{
    private Activity activity;
    private List<BusLines> list_bsl; 

    public MyCursorAdapter(Activity activity, List<BusLines> list_bsl){
        this.activity = activity;
        this.list_bsl = list_bsl;
    }

    public int getCount() {
        return list_bsl.size();
    }

    public Object getItem(int position) {
        return list_bsl.get(position);
    }

    public long getItemId(int position) {
        return list_bsl.get(position).getId();
    }

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

    View spinView;
    if( convertView == null ){
        LayoutInflater inflater = activity.getLayoutInflater();
        spinView = inflater.inflate(R.layout.spin_layout, null);
    } else {
         spinView = convertView;
    }
    TextView t1 = (TextView) spinView.findViewById(R.id.field1);
    TextView t2 = (TextView) spinView.findViewById(R.id.field2);
    t1.setText(String.valueOf(list_bsl.get(position).getLine_Num()));
    t2.setText(list_bsl.get(position).getName());
    return spinView;
    }
}

Unlike other solutions you find on the web, method getItemId establishes link with id field from database, just like SimpleCursorAdapter. That id is the argument passed in onItemSelected(AdapterView arg0, View arg1, int position, long id) in OnItemSelectedListener for spinner.setOnItemSelectedListener. Method getView inflated spin_layout.xml, identifies the two views contained in layout and assigns them values (as String!).

Solution 2

This is a simple example. Don't be fooled by the "cursor" name, it's just using a List. The idea is simple: extend from BaseAdapter and implement any missing methods (it's an abstract class); and don't forget to override the getView() method to provide the "visual" representation of your Category.

Share:
26,116
Daniel Kutik
Author by

Daniel Kutik

...

Updated on December 06, 2020

Comments

  • Daniel Kutik
    Daniel Kutik over 3 years

    I have an Android application with a Spinner and want to fill it dynamically with my own objects. The objects do exist already as a List<T>.

    The objects are of type Category:

    public class Category implements Serializable {
        private Long id;
        private String name;
    
        // constructors
        // getter & setter
        // hashCode, equals
        // toString
    }
    

    I know that I have to write a Adapter. How do I do that? I've tried to find some examples... no luck. Please advice.

  • Gangnus
    Gangnus almost 13 years
    The example fills spinner from an array from layout, not dynamically created. Such answers are for nothing!
  • M Granja
    M Granja almost 11 years
    SimpleCursorAdapter is on the android.support.v4 package, so with that you can use it clear back to API 4.