Sha*_*san 0 android android-spinner
我为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应该通过什么?
你ArrayList<Country>在SpinnerAdapter构造函数中传递了一个,但是你需要传递一个Country[].
这就是它给你一个错误的原因.
更改构造函数或传递Country[]而不是ArrayList<Country>
| 归档时间: | 
 | 
| 查看次数: | 209 次 | 
| 最近记录: |