create adapter to fill Spinner with objects [close

2019-05-11 13:56发布

问题:

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.

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



回答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.



回答3:

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.