2011-06-27 46 views
3

我有一個Android應用程序,其中包含Spinner,並且想用自己的對象動態填充它。這些對象確實已經作爲List<T>存在。創建適配器以將對象填充到微調器

的對象是Category類型:

public class Category implements Serializable { 
    private Long id; 
    private String name; 

    // constructors 
    // getter & setter 
    // hashCode, equals 
    // toString 
} 

我知道,我必須寫一個適配器。我怎麼做?我試圖找到一些例子...沒有運氣。請指教。

+0

查看以下答案,瞭解如何在不創建* CustomAdapter *的情況下執行此操作的完整解決方案:http://stackoverflow.com/a/21169383/293280 –

回答

12

這是我的5美分。我有類似的問題。我正在使用實現接口SpinnerAdapter的SimpleCursorAdapter,但是直到SDK版本11(Android 3.0)纔到達。我打算我的應用程序使用SDK 8(Android 2.2)以上,所以我不得不用另一個或我自己的SimpleCursorAdapter。真正的挑戰者是我還爲微調器使用了自定義XML佈局,並在其中顯示了來自遊標的多個字段,即遊標適配器。所以這裏是經過大量研究後的解決方案,而且信息並不容易。

這是在一個名爲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> 

這裏是實現SpinnerAdapter和延長(使用作爲一個小幫手)BaseAdapter適配器。最初使用的Cursor被轉換爲List,並在構造函數中傳遞,並與包含微調器的活動一起傳遞。

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; 
    } 
} 

不像在網絡上找到其他的解決方案,方法getItemId建立與ID從數據庫字段,就像SimpleCursorAdapter鏈接。該ID是onItemSelected(AdapterView arg0,View arg1,int position,long id)在OnItemSelectedListener中傳遞的參數spinner.setOnItemSelectedListener。方法getView inflated spin_layout.xml,標識佈局中包含的兩個視圖併爲它們賦值(如String!)。

+0

感謝分享此 – rmirabelle

+0

SimpleCursorAdapter位於android.support.v4包中,因此可以使用它清除回API 4。 –

1

這是一個simple example。不要被「光標」名稱所愚弄,它只是使用一個List。這個想法很簡單:從BaseAdapter擴展並實現任何缺少的方法(這是一個抽象類);並且不要忘記覆蓋getView()方法以提供Category的「可視」表示。