android监听器内的Java层次结构

时间:2012-06-21 04:58:26

标签: java android

在我的Android应用程序中,我想解决以下场景。

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()
    }
}

问题是我尝试从createSpinner方法中的'onItemSelected'列表器访问spinnerItemSelectedEvent(parent,view,pos,id)  我收到了以下错误。

方法spinnerItemSelectedListener(AdapterView,View,int,long)未定义类型为Activity

但是在列表器外面,对该方法的访问可以正常工作(忽略参数列表)。这背后的原因是什么?是否有任何替代方法来解决这个问题?请帮忙

2 个答案:

答案 0 :(得分:2)

android中的Activity代表一个屏幕。您正在创建objActivity类型的变量Activity,而它应该是Login类型,其中声明了函数spinnerItemSelectedEvent()。更改以下行:

private Activity  objActivity;

private Login  objActivity;

并且您的代码应该运行。

修改

拥有BaseActivity并让您的所有其他活动扩展此BaseActivity。要使函数spinnerItemSelectedEvent()可重用,请在BaseActivity中声明它,您可以按照现在尝试的方式使用它。

示例:

class BaseActivity extends Activity{
    public void spinnerItemSelectedEvent(AdapterView<?> parent, View view,
    int pos, long id){ }
} 

  class Login extends BaseActivity{
   public void spinnerItemSelectedEvent(AdapterView<?> parent, View view,
    int pos, long id){ //Local implementation of the function}
}

class LayoutBuilder {
private BaseActivity objActivity;

}

答案 1 :(得分:2)

问题是在LayoutBuilder内,您已将objActivity声明为Activity。声明它是Login,一切都应该没问题:

class LayoutBuilder {
    private Login objActivity;

    public LayoutBuilder(Login a) {
        objActivity = a; 
    }
    . . .

修改

如果您希望您的LayoutBuilder类可以被其他活动重用,那么一种方法是通过接口声明。例如:

public interface SpinnerSelectionHandler {
    void spinnerItemSelectedEvent(AdapterView<?> parent, View view,
        int pos, long id);
}

然后将Login声明为:

public class Login extends Activity implements SpinnerSelectionHandler {
    . . .

最后,重新定义LayoutBuilder以在其构造函数中使用SpinnerSelectionHandler

class LayoutBuilder {
    private Activity objActivity;
    private SpinnerSelectionHandler selectHandler;

    public LayoutBuilder(Activity a, SpinnerSelectionHandler handler) {
        objActivity = a;
        selectHandler = handler;
    }

然后替换

objActivity.spinnerItemSelectedEvent(parent,view,pos,id);

spinnerHandler.spinnerItemSelectedEvent(parent,view,pos,id);

另外,如果您不需要其他任何Activity引用,您可以从LayoutBuilder类中删除它。