• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Accessing view from SelectedListener

Hi:

I'm working through the "Hello View" tutorial on the developer.android website, and am looking for a best practice on how to do something.

My layout has a Spinner and an ImageView. When I select something for the spinner, I want to change the image in the ImageView.

So, in the Activity onCreate, I establish the listener for a spinner with spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

Now, within the listener, I need to "get a handle" for the ImageView.
As a crude solution, I successfully tried to

1. Create a public attribute in the Activity with:
public static Test2Activity act;

2. In the Activity onCreate, set it as:
act = this;

3. In the listener did:
ImageView imageView = (ImageView) Test2Activity.act.findViewById(R.id.birdimage);

4. so I could then change image with:
imageView.setImageResource(R.drawable.harpy);

Seems there's a better way! Any suggestions? Thanks!

Brian
 
Hi,

your solution is a little...mh circumstantial.

Within the onAction() method of your listener you can just call
Code:
findViewById(123);
or to make it more obvious

Code:
outerclass.this.findViewById(123);
You really don't want to create a public static object if it's just used within non-static methods in a private context :)

Greetings
 
Thanks for helping out!

I tried that (first), but get

The method findViewById(int) is undefined for the type MyOnItemSelectedListener

(Which is what led me to my known, ah, circumstantial hack...:))

Following is the whole piece of code in the listener. I know I'm missing something obvious, but just don't see it. Note the working piece is commented out.

package com.tests.test2;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class MyOnItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {

String selectedBird = parent.getItemAtPosition(pos).toString();

//ImageView imageView = (ImageView) Test2Activity.act.findViewById(R.id.birdimage);

ImageView imageView = findViewById(R.id.birdimage);
gives error The method findViewById(int) is undefined for the type MyOnItemSelectedListener

if (selectedBird.equals("Harpy Eagle")) {
imageView.setImageResource(R.drawable.harpy);
} else if (selectedBird.equals("Osprey")) {
imageView.setImageResource(R.drawable.osprey);
} else if (selectedBird.equals("Great Horned Owl")) {
imageView.setImageResource(R.drawable.greathorned);
} else {
Toast.makeText(parent.getContext(), selectedBird + " Not Defined", Toast.LENGTH_LONG).show();
}
}


public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
 
OK... I'm thinking that my listener needs to extend my activity, and be

public class MyOnItemSelectedListener extends Test2Activity implements OnItemSelectedListener

Doh!
 
OK... I'm thinking that my listener needs to extend my activity, and be

public class MyOnItemSelectedListener extends Test2Activity implements OnItemSelectedListener

Doh!

i am not sure this will do you any good. extending your activity will only do you good if you run MyOnItemSelectedListener as your main activity. you better do this:
public class Test2Activity extends Activity implements OnItemSelectedListener

this way, you can access your layout simply by findViewByID.
another way is to define your listener as an inner class of Test2Activity, and then you can access the layout by writing: Test2Activity.this.findViewById()
 
Hi,

you should try to understand the background of good program design. I think you focus too much on writing code as you seem to be a little helpless when it comes to design issues :)

Your attempt is circumstantial again ;)

As urid said, you either let your activity implement a listener or define your listener as anonymous class inside your setListener method.

Code:
public class myActivity extends Activity {

private void foo() {

myView.setOnItemSelectedListener(new onItemSelectedListener() {

public void onAction(View v) {
myActivity.this.findViewById(123);
}

});

}

}
If you want to have a dedicated class for your listener, you would pass your activity to it's constructor instead of accessing it in a static way.

Code:
public class CustomListener implements OnItemSelected Listener {
private Activity acc;

public CustomListener(Activity acc) {
this.acc = acc;
}

...
}
which would be instantiated in your activity class as followed

Code:
public class myActivity extends Activity {

private void foo() {

CustomListener custom = new CustomListener(this);
myView.setOnItemSelectedListener(custom);

}

Hope this helps :)
 
Back
Top Bottom