Java – Android spinner in the button onclicklistener

Android spinner in the button onclicklistener… here is a solution to the problem.

Android spinner in the button onclicklistener

I’m using a button that was created programmatically from a java file. I’m using the button onclick listener to represent functionality. Is it possible to add a spinner to the button click listener? My code is placed 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 the following error:

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

Appreciate any help and acknowledge in advance

Solution

Try replacing it with this

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

Related Problems and Solutions