Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top