如何将微调器项从复合控件android微调器类提取到主活动?

如何将微调器项从复合控件android微调器类提取到主活动?,android,android-layout,android-activity,Android,Android Layout,Android Activity,我试图从在主活动上呈现的自定义复合微调器中获取项目值。在输出中,可以选择微调器的下拉列表,但项目捕获没有发生。请给我下面的解决方案是代码片段 自定义/复合控件类 public class CompounControlSpinner extends Spinner { private String[] items = { "Google", "IBM", "Microsoft", "Accenture", "Cisco", "Dell", "Oracle", "

我试图从在主活动上呈现的自定义复合微调器中获取项目值。在输出中,可以选择微调器的下拉列表,但项目捕获没有发生。请给我下面的解决方案是代码片段

自定义/复合控件类

 public class CompounControlSpinner extends Spinner {
    private String[] items = { 
            "Google", "IBM", "Microsoft", "Accenture", "Cisco", "Dell", "Oracle", "Facebook"
    };
    public ComContSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setAdapter(new ArrayAdapter(context, android.R.layout.simple_spinner_item, items));
        this.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }

        });
    }

}
当我选择主活动中应该动态更新的项目时,请任何人建议我如何从spinner获取主活动中的数据。提前谢谢

…当我选择该项目时,它应在main中动态更新 活动

当您在自定义视图中获得活动引用时,您可以简单地使用它作为接口,在发生选择事件时与之通信并传递所选值。您可以如下所示:

public interface SelectionHappened {

    void onSelection(String newSelectedItem);
}
活动需要实现此接口:

public class MainActivity extends ActionBarActivity implements SelectionHappened {
    //... current code

    @Override
    public void onSelection(String newSelectedItem) {
         // when this method is triggered the user selected something in the Spinner   
    }

}
最后一项是从自定义视图发送事件:

public class CompounControlSpinner extends Spinner {
    private SelectionHappened mListener; 
    private String[] items = { 
            "Google", "IBM", "Microsoft", "Accenture", "Cisco", "Dell", "Oracle", "Facebook"
    };
    public ComContSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        mListener = (SelectionHappened) context;
        this.setAdapter(new ArrayAdapter(context, android.R.layout.simple_spinner_item, items));
        this.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                  mListener.onSelection((String)parent.getAdapter().getItem(position)); // send the event to the activity 
            }
        //... rest of the current code

在复合类中,onselection with param抛出空指针异常,但字符串不是空的。@user2627142将我的代码中的行分为几行,然后查看在哪一行上获得了类似异常的内容。如果您遵循我的代码,则不会发生该异常。09-30 14:27:36.654:E/AndroidRuntime1188:FATAL异常:main 14:27:36.654:E/AndroidRuntime1188:java.lang.NullPointerException 09-30 14:27:36.654:E/AndroidRuntime1188:at com.mysmax.componControlSpinner$1.OnItemSelectedComConControlSpinner.java:30 14:27:36.654:E/AndroidRuntime1188:at widget.AdapterView.fireOnSelectedAdapterView.java:893 09-30 14:27:36.654:E/AndroidRuntime1188:at android.widget.AdapterView.access$200AdapterView.java:48 09-30 14:27:36.654:E/AndroidRuntime1188:at android.widget.AdapterView$SelectionNotifier.runAdapterView.java:861I使用您拥有的新代码进行了检查共享。谢谢但它失败了。任何其他解决方案。@user2627142我已经告诉过你使用上面的代码来查看哪一行抛出了异常,而不是有一个大行。发布stacktrace并没有告诉我任何事情,因为我看不到ComContSpinner中的第30行和空行。
public class MainActivity extends ActionBarActivity implements SelectionHappened {
    //... current code

    @Override
    public void onSelection(String newSelectedItem) {
         // when this method is triggered the user selected something in the Spinner   
    }

}
public class CompounControlSpinner extends Spinner {
    private SelectionHappened mListener; 
    private String[] items = { 
            "Google", "IBM", "Microsoft", "Accenture", "Cisco", "Dell", "Oracle", "Facebook"
    };
    public ComContSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        mListener = (SelectionHappened) context;
        this.setAdapter(new ArrayAdapter(context, android.R.layout.simple_spinner_item, items));
        this.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                  mListener.onSelection((String)parent.getAdapter().getItem(position)); // send the event to the activity 
            }
        //... rest of the current code