0

Write a stand along interface or not like this,

spinner.setAdapter(this.mAdapter); 
OnItemSelectedListener spinnerListener = new OnItemSelectedListener(this,this.mAdapter);  
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){ 
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(parent.getContext(),"The planet is "+  parent.getItemAtPosition(pos).toString(),Toast.LENGTH_LONG).show();  
textViewa.setText("You choose :"+ " " + mAdapter.getItem(arg2));
arg0.setVisibility(View.VISIBLE);    
 }    
 public void onNothingSelected(AdapterView<?> arg0) {    
// TODO Auto-generated method stub  
    textViewa.setText("NONE");
            arg0.setVisibility(View.VISIBLE);    
     }    
}); 

and this one,

public class SpinnerActivity extends Activity implements OnItemSelectedListener {     ...  
    public void onItemSelected(AdapterView<?> parent, View view,  int pos, long id) {         // An item was selected. You can retrieve the selected item using         // parent.getItemAtPosition(pos)     }      public void onNothingSelected(AdapterView<?> parent) {         // Another interface callback     } }

what's different?

codeMagic
  • 44,549
  • 13
  • 77
  • 93
learner1
  • 123
  • 2
  • 3
  • 13
  • One is directly, another with an implements. which is more standard written? thannks – learner1 Dec 06 '13 at 15:35
  • 1
    this is really a matter of preference IMO, both are perfectly acceptable – tyczj Dec 06 '13 at 15:38
  • 1
    I use the first if there is only one `Spinner` and the second with multiple `Spinner`s then `switch` on the `parent` selected. But yes, just preference, they both do the same thing. – codeMagic Dec 06 '13 at 15:41
  • Thank you very much @tyczj and codeMagic for your succinct summary. – learner1 Dec 08 '13 at 10:43

3 Answers3

1

what's different?

The first option creates an anonymous inner class that implements the interface. This is useful when you need multiple OnItemSelectedListener implementations that differ significantly.

In the second option, your Activity implements OnItemSelectedListener, so (assuming that you pass this to each of your Spinners) every Spinner will go to the same OnItemSelectedListener instance.

which is more standard written?

It depends on your personal preference and your use case. If all of your Spinners have similar behavior or simple enough behavior that onItemSelected() will be short, then the latter option would work fine.

However, if each of your Spinners is highly unique and share little common code in onItemSelected(), then creating a new OnItemSelectedListener instance for each Spinner might be a better design decision.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
1

The benefit of making Activity implement an Interface is that you will have one object less to allocate and collect. But making anonymous implementation is not a big drawback. As tyczj said, it's really a matter of preference. If it does something like triggering visibility I would make it anonymous so you don't have to search the overridden method in Activity class.

Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
1

They are both equivalent and like tyczj says it is a matter of preference. The reason why sometimes implementing the interface instead of creating an anonymous inner class is preferred is for example if you have multiple Buttons from which you want to get their click events. If you define an anonymous inner class for each of them you are creating extra Objects. But as I said before, both patterns are functionally the same.

Emmanuel
  • 13,083
  • 4
  • 39
  • 53