Spinner adapter isnt working

I have created a custom adapter class for spinner, which can take object type values. But weirdly its showing some error in code.

SpinAdapter class

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

Activity

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

}
}

The problem is showing when i am trying to make a instance of new SpinAdapter inside onCreate().

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

Its showing error for this keyword. how am i going to fix it? what should be the COntext to pass if its not this?

Answers


You are passing an ArrayList<Country> in your SpinnerAdapter constructor but you need to pass a Country[].

This is the reason it is giving you an error.

Either change your constructor or pass Country[] instead of ArrayList<Country>


Need Your Help

C++ Class holding and returning pointer to another class

c++ class pointers arduino

I've been trying to figure out how a Class can store and retrieve a Class pointer.(?)

Using same project with mutiple configuration projects in iOS?

ios xcode xcode5 development-environment iphone-developer-program

I am making an app for my 5 clients. The only difference is icons and webservices. I don't want to make 5 different separate apps with same source code. What i am thinking to do is that i create one