java Android微调器“onclicklistener”问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4287466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:42:16  来源:igfitidea点击:

Android spinner "onclicklistener" question

javaandroid

提问by brux

I am working with Spinner, cursors and adapters.

我正在使用微调器、游标和适配器。

I want to setup a click listener for the spinner so that once a user selects an item from the spinner it gets the current selected item and then carrys out some other tasks ( all this extra code is fine, its just 1 problem I am having).... It kind of works, however, once I declare the setOnItemSelectedListener callback, since the cursor has already been populated, the event is fired as soon as the app launches.

我想为微调器设置一个点击侦听器,这样一旦用户从微调器中选择一个项目,它就会获取当前选定的项目,然后执行一些其他任务(所有这些额外的代码都很好,这只是我遇到的 1 个问题) .... 然而,一旦我声明了 setOnItemSelectedListener 回调,它就会起作用,因为光标已经被填充,一旦应用程序启动,事件就会被触发。

I guess I need a way to define the cursor without selecting an initial item so that the event doesnt fire (since an item will not be selected). Or is there another better way to achieve this?

我想我需要一种方法来定义光标而不选择初始项目,以便不会触发事件(因为不会选择项目)。还是有另一种更好的方法来实现这一目标?

Basically, as it stands, once the app loads the setOnItemSelectedListener function is firing because the cursor is being populated ( i think). Moreover, ignoreing the fact that the event is firing too soon, if I then select the -same- item in the spinner, it doesnt fire the event sincethe item didnt change. SHould I be using a different callback instead of setonitemslectedlistener? Here is the code I have so far.

基本上,就目前而言,一旦应用程序加载 setOnItemSelectedListener 函数就会触发,因为正在填充光标(我认为)。此外,忽略事件触发过早这一事实,如果我然后在微调器中选择 -same- 项目,它不会触发事件,因为该项目没有改变。我应该使用不同的回调而不是 setonitemslectedlistener 吗?这是我到目前为止的代码。

            c = db.getallrecents();
    startManagingCursor(c);

    busnumspinner = (Spinner) findViewById(R.id.Spinner01);

        SimpleCursorAdapter spinneradapter = new SimpleCursorAdapter(this,
                R.layout.lvlayout, c, spincol, spinto);

    busnumspinner.setAdapter(spinneradapter);


    busnumspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            String spinnerString = null;
            Cursor cc = (Cursor)(busnumspinner.getSelectedItem());
            if (cc != null) {
                spinnerString = cc.getString(
                    cc.getColumnIndex("busnum"));
                text = spinnerString;
            }

            showDialog(DATE_DIALOG_ID);

        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    });

回答by Javi

This has already been discussed in this question. Look there, though it has a similar answer like the one given by blindstuff.

这已经在这个问题中讨论过了。看看那里,虽然它有一个类似于盲人给出的答案。

EDIT:

编辑:

If the onItemSelectedListener is not firing when you need it, then you probably need a onClickListener in eachtext item of the droplist and get in there the current position of the selected item of the spinner. The problem is that as it is said herespinner don't support this event, but maybe you can get it by doing something similar to the explained in this stackoverflow question. I haven't tried it so I'm not sure it will work.

如果 onItemSelectedListener 在您需要时没有触发,那么您可能需要在下拉列表的每个文本项目中使用 onClickListener 并在那里获取微调器所选项目的当前位置。问题是,正如这里所说的Spinner 不支持此事件,但也许您可以通过执行类似于此stackoverflow question 中解释的操作来获得它。我还没有尝试过,所以我不确定它会起作用。

回答by blindstuff

Use a boolean flag to ignore the first time it gets selected by the system, its not a pretty solution, but i've struggled with this a couple of times, and never found a better solution.

使用布尔标志来忽略系统第一次选择它时,这不是一个很好的解决方案,但我已经为此挣扎了几次,但从未找到更好的解决方案。

回答by Manjeet

you can add first item of spinner by default value like selectvalues and check its position in onitemselected listener, if it's zero position then dont enter in the loop greater than 0 then enter in the method

您可以通过默认值添加微调器的第一项,例如 selectvalues 并检查其在 onitemselected 侦听器中的位置,如果它的位置为零,则不要进入大于 0 的循环,然后进入该方法

see the example

看例子

 busnumspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int   
    position, long id) {
     if(position!=0)
     {
        String spinnerString = null;
        Cursor cc = (Cursor)(busnumspinner.getSelectedItem());
        if (cc != null) {
            spinnerString = cc.getString(
                cc.getColumnIndex("busnum"));
            text = spinnerString;
        }
      }

    So this is not run the default value

回答by OferR

Try this:

试试这个:

Extend your SimpleCursorAdapter, override bindView() and set OnClickListener for the row view.

扩展您的 SimpleCursorAdapter,覆盖 bindView() 并为行视图设置 OnClickListener。

This will overcome both issues: You do not get the initial call, and you get each selection click (inc. re-selection)

这将克服两个问题:您不会收到初始调用,并且每次选择都会点击(包括重新选择)

Let me know if you need example code.

如果您需要示例代码,请告诉我。

EDIT: Code example:

编辑:代码示例:

protected class NoteAdapter extends SimpleCursorAdapter {

    // Constructor
    public NoteAdapter(Context context, Cursor c) {
        super(context, R.layout.etb_items_strip_list_item, c, fromNote, toNote);
    }


    // This is where the actual binding of a cursor to view happens
    @Override
    public void bindView(View row, Context context, Cursor cursor) {
        super.bindView(row, context, cursor);

        // Save id
        Long id = cursor.getLong(cursor.getColumnIndex("_id"));
        row.setTag(id);

        // Set callback
        row.setOnClickListener(row_OnClick);
    }

    // Callback: Item Click
    OnClickListener row_OnClick = new OnClickListener(){
        public void onClick(View v) {
            Long id = (Long) v.getTag();
        }
    };

}