简体   繁体   English

如何单击按钮以启动微调器控件的ItemSelected事件?

[英]How can I click a button to launch an ItemSelected event of spinner control?

I have a spinner control with setOnItemSelectedListener in my app, when I select an item of the spinner, the event onItemSelected will be launched. 我的应用程序中有一个带有setOnItemSelectedListener的微调器控件,当我选择微调器的一个项目时,将启动onItemSelected事件。 Now I hope to click a button to launch the onItemSelected event, how can I do? 现在,我希望单击一个按钮以启动onItemSelected事件,该怎么办? Thanks! 谢谢!

spinnerFolder.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
                //Do Business Logic
            }           
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }   
        });

Just call the following from your button listener: 只需从您的按钮监听器中调用以下命令:

spinnerFolder.getOnItemSelectedListener().onItemSelected(spinnerFolder, spinnerFolder.getSelectedView(), spinnerFolder.getSelectedItemPosition(), spinnderFolder.getSelectedItemId());

That's all :-) 就这样 :-)

The third parameter (int arg2) of onItemSelected for your spinner is the position, so you can get the current selection by 微调器的onItemSelected的第三个参数(int arg2)是位置,因此可以通过以下方式获取当前选择

String selection = (String) mSpinnerAdapter.getItem(position);

or use mSpinner.getItemAtPosition(position) if you don't have a custom adapter. 或使用mSpinner.getItemAtPosition(position)如果您没有自定义适配器)。

Store the current selection somewhere and pick it up in the onClickListener for your button. 将当前选择存储在某处,并在onClickListener中为您的按钮选择。

When you populate the spinner with some data, you will have some sort of list objects (Strings, custom object, whatever) in the SpinnerAdapter , you'll keep a reference to that list, let's call it: private List<Object> dataList = ... 当用一些数据填充微调器时,在SpinnerAdapter中将有某种列表对象(字符串,自定义对象等),您将保留对该列表的引用,将其称为: private List<Object> dataList = ...

First of all I would create a method that would handle the specific data object: protected void doBusinessLogic(Object myObj) { // do the things that make you happy } 首先,我将创建一个处理特定数据对象的方法:protected void doBusinessLogic(Object myObj){//做让您开心的事情}

Then call this from listener as: 然后从侦听器中将此调用为:

spinnerFolder.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)    {
                // args2 is the position from backed data list
                doBusinessLogic(dataList.get(args2));
            }           
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }   
        });

Then from button listener you would call above method again, but the object index you would get is from spinnerFolder.getSelectedItemPosition(); 然后,您将从按钮侦听器再次调用上述方法,但您将获得的对象索引来自spinnerFolder.getSelectedItemPosition(); :

myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int dataIndex = spinnerFolder.getSelectedItemPosition();
                doBusinessLogic(dataList.get(index));
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM