4

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.

Daniel Kutik
  • 6,997
  • 2
  • 27
  • 34
  • Check out the following answer for a full solution on how to do this without creating a *CustomAdapter*: http://stackoverflow.com/a/21169383/293280 – Joshua Pinter Jan 16 '14 at 18:07

3 Answers3

12

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!).

MSquare
  • 6,311
  • 4
  • 31
  • 37
  • SimpleCursorAdapter is on the android.support.v4 package, so with that you can use it clear back to API 4. – M Granja Aug 15 '13 at 14:19
1

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.

dmon
  • 30,048
  • 8
  • 87
  • 96
-1

I am not sure if this helps, but the Android SDK has a nice little example of supplying data to a Spinner from an array. Converting a List to an array is trivial, so maybe this will point you in the right direction.

http://developer.android.com/resources/samples/Spinner/src/com/android/example/spinner/SpinnerActivity.html

Good luck.

duanemat
  • 237
  • 1
  • 6
  • 12
  • The example fills spinner from an array from layout, not dynamically created. Such answers are for nothing! – Gangnus Sep 23 '11 at 09:02