0

I am trying to use a spinner and a button in my app. I want to know if my syntax for implementing multiple interfaces is correct. This is how it is currently set up:

public class MainActivity extends AppCompatActivity
    implements View.OnClickListener, AdapterView.OnItemSelectedListener

My @Override for the onItemSelectedListener is also not being accepted. I'll post what I have but i will leave out the part that handles what happens after the button is clicked, as I there are no errors there and i believe it has no bearing on this problem:

public class MainActivity extends AppCompatActivity
    implements View.OnClickListener, AdapterView.OnItemSelectedListener {

Button setCountdown;
Spinner higestNotification;
EditText countdownTime;
int countdownConversion; //convert contents of countdownTime to an integer
int clockStart = 1;
int[] notificationTimes = new int[7];
String convertNotificationTimeToString;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setCountdown = (Button) findViewById(R.id.setCountdown);
    higestNotification = (Spinner) findViewById(R.id.highestNotification);
    countdownTime = (EditText) findViewById(R.id.countdownTime);


    setCountdown.setEnabled(false);//disable button at the start of the app
    higestNotification.setEnabled(false);// disable spinner at the start of the app


    countdownTime.setOnClickListener(this);
    setCountdown.setOnClickListener(this);

    higestNotification.setOnItemSelectedListener(this);
}

@Override
public void OnItemSelected(AdapterView<?> arg0, View arg1, int position, long id )

{

}

@Override
public void OnNothingSelected(AdapterView<?> arg0)
{

}

as i said, the errors are when i try to implement View.OnClickListener and AdapterView.OnItemSelectedListener, as well as the overrides for OnItemSelected

2
  • Your overridden methods should start with a lower-case letter. As it's specified now in your code, the method signatures don't match the interface. Commented Aug 1, 2017 at 3:38
  • thanks! i feel stupid now haha Commented Aug 1, 2017 at 3:42

2 Answers 2

1

Your overridden methods should start with a lower-case letter.

As it's specified now in your code, the method signatures don't match the interface contract.

Try instead something like:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}

@Override
public void onClick(View view) {
}

As an aside, Android Studio provides a shortcut for overriding interface methods on your behalf: Control + O

Sign up to request clarification or add additional context in comments.

Comments

0

Your method OnItemSelected is having upper case letter O which is not same as onItemSelected.

I tried this in my android-studio and it works.

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("TEST", "onCreate");
    }

    @Override
    public void onClick(View view) {
        // handle onClick        
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        // handle onItemSelected
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
        // handle onNothingSelected

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.