实现自定义适配器

时间:2011-03-28 04:48:03

标签: android

我正在创建一个自定义适配器,简单的东西,它在adpater的public int getCount()上爆炸。我不知道我错过了什么?

public class GREVocabulary extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // set the color mlayout.setBackgroundColor(Color.WHATEVER);
        setContentView(R.layout.main);
        List<String> alphabetslsit = Arrays.asList(getResources().getStringArray(R.array.alphabet));    
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        try{
        AlphabetAdapter adapter = new AlphabetAdapter(this,alphabetslsit);


        spinner.setAdapter(adapter);
        }
        catch (Exception e)
        {
            Log.v(e.toString(),e.getMessage());
        }

    }
}

public class AlphabetAdapter extends BaseAdapter implements SpinnerAdapter
 {


    private List<String> aplhabetlist;
    private Context context;

    public AlphabetAdapter(Context context, List<String> aplhabetlist ) { 
        this.context = context;
        this.aplhabetlist = aplhabetlist;
    }

    @Override
    public int getCount() 
    {

         return aplhabetlist.size();
    }

    @Override
    public Object getItem(int arg0) {
        return aplhabetlist.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) 
    {
        View view = super.getDropDownView(arg0, arg1, arg2);
        //if (arg0 % 2 == 0) 
        //{
        //  view.setBackgroundColor(Color.BLUE);
        //}

        return view; 

    }

}

在我获得的例外之后

// Stack: 2, Locals: 2
  public bridge synthetic void setAdapter(android.widget.Adapter x0);
    0  aload_0 [this]
    1  aload_1 [x0]
    2  checkcast android.widget.SpinnerAdapter [7]
    5  invokevirtual android.widget.AbsSpinner.setAdapter(android.widget.SpinnerAdapter) : void [8]
    8  return
      Line numbers:
        [pc: 0, line: 2]
      Local variable table:
        [pc: 0, pc: 9] local: this index: 0 type: android.widget.AbsSpinner
        [pc: 0, pc: 9] local: x0 index: 1 type: android.widget.Adapter

  // Method descriptor #67 ()Landroid/widget/Adapter;
  // Stack: 1, Locals: 1
  public bridge synthetic android.widget.Adapter getAdapter();
    0  aload_0 [this]
    1  invokevirtual android.widget.AbsSpinner.getAdapter() : android.widget.SpinnerAdapter [9]
    4  areturn
      Line numbers:
        [pc: 0, line: 2]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: android.widget.AbsSpinner

  Inner classes:
    [inner class info: #36 android/view/ViewGroup$LayoutParams, outer class info: #83 android/view/ViewGroup
     inner name: #37 LayoutParams, accessflags: 9 public static]

1 个答案:

答案 0 :(得分:1)

BaseAdapter已经实现了SpinnerAdapter。所以不需要明确地实现SpinnerAdapter。

HTH!