Я создал собственный класс адаптера для 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);
}
}
Проблема показывает, когда я пытаюсь сделать экземпляр нового SpinAdapter внутри OnCreate().
adapter = new SpinAdapter(this, android.R.layout.simple_spinner_item, countries);
Его показывая ошибку для этого ключевого слова. как я его исправлю? какой должен быть COntext, если его не это?
'this' должно быть хорошо. Какая ошибка? – Raghunandan
Конструктор SpinAdapter (CityActivity, int, ArrayList) не определено –
Apporv ответил на ваш вопрос. 'this' не имеет ничего общего с вашей ошибкой – Raghunandan