In my android application I want to solve the following scenario.
class Login extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutBuilder objLB=new LayoutBuilder(this);
objLB.createSpinner();
}
public void spinnerItemSelectedEvent(AdapterView<?> parent, View view,
int pos, long id)
{
}
}
class LayoutBuilder {
private Activity objActivity;
public LayoutBuilder(Activity a) {
objActivity = a;
}
public void createSpinner() {
final Spinner objSPItem = new Spinner(objActivity);
objSPItem.setOnItemSelectedListener(
new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id)
{
// Do some common activity
objActivity.spinnerItemSelectedEvent(parent,view,pos,id);
// calling this for do some additional task
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
objActivity.spinnerItemSelectedEvent()
}
}
and the problem is when i try to access spinnerItemSelectedEvent(parent,view,pos,id) from the ‘onItemSelected’ listner inside createSpinner method
I got the following error.
The method spinnerItemSelectedListener(AdapterView, View, int, long) is undefined for the type Activity
but out side the listner the access to the method works ok(neglect parameter list). What is the reason behind that? is any alternate way exist for solving this? plz help
Activity in android represents a screen. You are making a variable
objActivityof typeActivitywhereas it should be of typeLoginwhere the functionspinnerItemSelectedEvent()is declared. Change the following lines:to
and your code should run.
EDIT
Have a
BaseActivityand let all your other activities extend thisBaseActivity. To make the functionspinnerItemSelectedEvent()reusable declare it in theBaseActivityand you can use it the way you are trying to do it now.Example: