Pattabi Raman
Pattabi Raman

Reputation: 5854

Android spinner within a button onclicklistener

I am using a button created programmatically through java file. I am using button onclick listener to represent the functionality. Is it possible to add a spinner inside that button click listener? My Code goes here:

View.OnClickListener newtodobtn = new View.OnClickListener() {
    public void onClick(View v) {
      // it was the 1st button

        setContentView(R.layout.main);

        sp1 = (Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.priority, R.id.spinner1);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp1.setAdapter(adapter);
        sp1.setAdapter(adapter);}

If i use this code, i get following error:

The method createFromResource(Context, int, int) in the type ArrayAdapter is not applicable for the arguments (new View.OnClickListener(){}, int, int)

Any help is appreciated and thanks in advance

Upvotes: 3

Views: 1996

Answers (2)

Jyosna
Jyosna

Reputation: 4446

instead of this u have to specify <class name.this>. Bcz if u provide only this then it will refer to button. Thats why its not working.

Upvotes: 2

Adil Soomro
Adil Soomro

Reputation: 37729

Try Replacing by this

ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(YourActivity.this, R.array.priority, R.id.spinner1);

Upvotes: 3

Related Questions