views:

150

answers:

3

Hello, I'm abit new to programming Android App's, however I have come across a problem, I can't find a way to make global variables -unlike other coding like php or VB.NET, are global variables possible? If not can someone find a way (and if possible implement the way into the code I will provide below) to get a value from the variable 'songtoplay' so I can use in another Public Void...

Here is the code:

    final Spinner hubSpinner = (Spinner) findViewById(R.id.myspinner);
    ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.colours, android.R.layout.simple_spinner_item); adapter
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    hubSpinner.setAdapter(adapter);
    //
    hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
             //code
          Object ttestt = hubSpinner.getSelectedItem();
          final String test2 = ttestt.toString();
          Toast message1 = Toast.makeText(Textbox.this, test2, Toast.LENGTH_LONG);
        message1.show();    
        String songtoplay = test2;
          // Need songtoplay to be available in another 'Public Void'
        }
        public void onNothingSelected(AdapterView<?> parentView) {
             //Code
        }
    });

Basically, it gets the value from the Spinner 'hubSpinner' and displays it in a Toast. I then want it to return a value for string variable 'songtoplay' -or find a way to make it global or useable in another Public Void, (Which is will a button, -loading the song to be played)

Please help me,

Thanks alot.

James

+4  A: 

Use an instance variable:

hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    private String songtoplay;
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
       // ...
       songtoplay = test2;
    }
    public void onNothingSelected(AdapterView<?> parentView) {
       // ...
       doSomethingWith(songtoplay);
    }
});

In Java, static variables are the equivalent of global variables, and they are best avoided like all global variables.

Esko Luontola
So how do I impletement 'songtoplay' into this other public void? pbutton6.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ImageView image6 = (ImageView) findViewById(R.id.test_image); image6.setImageResource(R.drawable.redstar); pbutton1.setVisibility(View.VISIBLE); pbutton6.setVisibility(View.INVISIBLE); String file = songtoplay;//I will then use this file in MediaPlayer.Create() } });I want the button to 'play the track' 'songtoplay' when clicked, but it's highlighted 'songtoplay cannot be resolved'? Can you fix this?
James Rattray
In that case make it an instance variable of the enclosing class. Non-static inner classes can access the members of the enclosing class (and vice versa). See http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
Esko Luontola
A: 

Don't forget about package access, too. Better than static variables for your case. After all, what 'static' really means in the Java context is a little different than in C: it means "there is only one instance of this thing in this class". That may or may not be what you really want.

In fact, it is quite possible that you will want to declare the same variable to both be static (so that there really is only one) and package access (so that any other class in the same package can see it).

But beware of globals: there is a REASON good software engineering practices, such as OOP/OOD, frown so much on globals these days. They are OK for small programs, sometimes exactly what you want, but even there, they can turn into booby traps. That is WHY instead of truly global variables, people use either package access or public variables embedded in/member of a publicly accessible class.

Matt J.
A: 

I would just have my class implement the OnItemSelectedListener interface. That way the onItemSelected() method can stash the string in the current object instance.

some really skeletal code:

class MyClass extends Activity implements OnItemSelectedListener {
    private String songtoplay;

    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
       // ...
       this.songtoplay = test2;
    }

    public void needsSongtoplay(...) {
        doSomething(this.songtoplay);
    }
}
rascalking
How though??? :)
James Rattray
updated my answer to include some skeleton code.
rascalking