Accessing view from SelectedListener


Last Updated:

  1. ShelbyZork

    ShelbyZork New Member This Topic's Starter

    Joined:
    Oct 23, 2010
    Messages:
    4
    Likes Received:
    0
    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
     

    Advertisement
  2. sscAndroid

    sscAndroid Member

    Joined:
    Oct 23, 2010
    Messages:
    17
    Likes Received:
    1
    Hi,

    your solution is a little...mh circumstantial.

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

    Code (Text):
    1.  
    2. 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
     
  3. ShelbyZork

    ShelbyZork New Member This Topic's Starter

    Joined:
    Oct 23, 2010
    Messages:
    4
    Likes Received:
    0
    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.
    }
    }
     
  4. ShelbyZork

    ShelbyZork New Member This Topic's Starter

    Joined:
    Oct 23, 2010
    Messages:
    4
    Likes Received:
    0
    OK... I'm thinking that my listener needs to extend my activity, and be

    public class MyOnItemSelectedListener extends Test2Activity implements OnItemSelectedListener

    Doh!
     
  5. urid

    urid New Member

    Joined:
    Oct 25, 2010
    Messages:
    1
    Likes Received:
    0
    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()
     
  6. sscAndroid

    sscAndroid Member

    Joined:
    Oct 23, 2010
    Messages:
    17
    Likes Received:
    1
    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 (Text):
    1.  
    2.  
    3. public class myActivity extends Activity {
    4.  
    5. private void foo() {
    6.  
    7. myView.setOnItemSelectedListener(new onItemSelectedListener() {
    8.  
    9. public void onAction(View v) {
    10. myActivity.this.findViewById(123);
    11. }
    12.  
    13. });
    14.  
    15. }
    16.  
    17. }
    18.  
    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 (Text):
    1.  
    2.  
    3. public class CustomListener implements OnItemSelected Listener {
    4. private Activity acc;
    5.  
    6. public CustomListener(Activity acc) {
    7. this.acc = acc;
    8. }
    9.  
    10. ...
    11. }
    which would be instantiated in your activity class as followed

    Code (Text):
    1.  
    2.  
    3. public class myActivity extends Activity {
    4.  
    5. private void foo() {
    6.  
    7. CustomListener custom = new CustomListener(this);
    8. myView.setOnItemSelectedListener(custom);
    9.  
    10. }
    Hope this helps :)
     

Share This Page

Loading...