2014-01-17 119 views
0

我爲微調器創建了一個自定義的適配器類,它可以獲取對象類型值。但奇怪的是它在代碼中顯示出一些錯誤。Spinner適配器不能正常工作

SpinAdapter類

public class SpinAdapter extends ArrayAdapter<Country> 
{ 
    private Context context; 
    private Country[] values; 

    public SpinAdapter(Context context, int textViewResourceId, Country[] values) 
    { 
     super(context, textViewResourceId, values); 
     this.context = context; 
     this.values = values; 
    } 

    public int getCount(){ 
     return values.length; 
    } 

    public Country getItem(int position){ 
     return values[position]; 
    } 

    public long getItemId(int position){ 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      TextView label = new TextView(context); 
     label.setTextColor(Color.BLACK); 
     label.setText(values[position].getName()); 
     return label; 
    } 
    @Override 
    public View getDropDownView(int position, View convertView, 
      ViewGroup parent) { 
     TextView label = new TextView(context); 
     label.setTextColor(Color.BLACK); 
     label.setText(values[position].getName()); 

     return label; 
    } 
} 

活動

public class CityActivity extends Activity { 

EditText cityNameTxtBox; 
EditText cityAboutTxtBox; 
EditText cityPopulationTxtBox; 

private Spinner mySpinner; 
    private SpinAdapter adapter; 
    ArrayList<Country> countries; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_city); 
    // Show the Up button in the action bar. 
    setupActionBar(); 

    Country aCountry = new Country("a","1"); 
    Country bCountry = new Country("b","2"); 
    Country cCountry = new Country("c","3"); 
    countries.add(aCountry); 
    countries.add(bCountry); 
    countries.add(cCountry); 

    adapter = new SpinAdapter(this, android.R.layout.simple_spinner_item, countries); 

    mySpinner = (Spinner) findViewById(R.id.countrySpinner); 
    mySpinner.setAdapter(adapter); 

} 
} 

的問題顯示了當我試圖讓裏面的onCreate新SpinAdapter的實例()。

adapter = new SpinAdapter(this, android.R.layout.simple_spinner_item, countries); 

它顯示了關鍵字錯誤。我該如何解決它?如果不是這個,應該通過什麼COntext?

+0

'this'應該罰款。什麼是錯誤? – Raghunandan

+0

構造函數SpinAdapter(CityActivity,int,ArrayList )未定義 –

+0

Apporv回答了您的問題。 '這個'與你的錯誤沒有任何關係 – Raghunandan

回答

5

您在構建函數SpinnerAdapter中傳入ArrayList<Country>,但您需要通過Country[]

這就是它給你一個錯誤的原因。

要麼改變你的構造函數或通過Country[],而不是ArrayList<Country>

+0

亞..感謝..我意識到,以後。 :-) –

+0

不客氣... – Apoorv