Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8944699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:05:01+00:00 2026-06-15T12:05:01+00:00

I think I am making a design error in my Android app somewhere. My

  • 0

I think I am making a design error in my Android app somewhere. My (simplified) code is pasted below.

I am using the writeMidi method in MainActivity. However, I would also like to use it, or actually just trigger it, when “onItemSelected” is triggered in the custom listener.

I am a bit torn on how to do that. Should I redesign this code to fit the customlistener in the main activity?

Thanks for any help.

public class MainActivity extends Activity{


    int song = 0;
    int[] music;
    public int instrument;
    public CustomOnItemSelectedListener listener;


    // *******************************************************
    // set Layout - on create
    // *******************************************************
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        instrument = 0;

        listener = new CustomOnItemSelectedListener();
        addListenerOnSpinnerItemSelection();

        //more stuff, including using the writeMidi method

};


    public void addListenerOnSpinnerItemSelection(){

        instrumentSp = (Spinner) findViewById(R.id.instrument);
        instrumentSp.setOnItemSelectedListener(listener);
    }


    public void writeMidi(int[] music, int count) {
        // so some stff
    }

}

and in a separate file;

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

    private int instrument = 0;

      public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        Toast.makeText(parent.getContext(), 
            "Please wait a minute for the instrument to be changed. ", Toast.LENGTH_SHORT).show();
         instrument = pos;

      }


      public int getInstrument(){
          return instrument;
      }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:05:02+00:00Added an answer on June 15, 2026 at 12:05 pm

    I tried a number of the solutions suggested, but could get none of them to fully work.

    So I solved it by not using a separate class like this:

    instrumentSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {   
         public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a specific game making framework but I think the question applies to
Here's the thing: I'm using the CommonJS way of making my mobile (iPhone/Android) application
For a 2D game I'm making (for Android) I'm using a component-based system where
I have the following SQL design issue. The code below might look a little
I'm trying to decide how to design a Netty app with what I think
I'm making a product review service using Google App Engine, and I'm wondering about
I think I am making a simple mistake, but since I noticed there are
Which do you think are best practices for making a windows dialog compatible both
I think I'm missing something very obvious and its making my brain hurt. class
I'm thinking about making a Hubot clock-based notifier (think It's 5, time to go

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.