首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在android中从微调器中删除重复项

如何在android中从微调器中删除重复项
EN

Stack Overflow用户
提问于 2018-07-11 07:56:39
回答 2查看 603关注 0票数 0

微调工具的代码如下所示,我的应用程序中的微调工具有时会出于某种奇怪的原因复制它的内容。如何防止这种情况发生?:

代码语言:javascript
复制
Spinner spinnerG = (Spinner) findViewById(R.id.spGroup);
    final ArrayAdapter<String> dataAdapterG = new ArrayAdapter<>
            (this, R.layout.simple_spinner_item, groups);
    dataAdapterG.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
    spinnerG.setAdapter(dataAdapterG);                                   //general basics       //sets up the group spinner, filled with the groups list

    spinnerG.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        {
            selectedGroup = groups.get(position);
            studentsToShow.clear();
            for(int i = 0; i < studList.size(); i++){
                if(studList.get(i).getGroup().equals(selectedGroup)){
                    Students a = new Students();
                    a.setFirstName(studList.get(i).getFirstName());
                    a.setLastName(studList.get(i).getLastName());
                    a.setStudentID(studList.get(i).getStudentID());
                    a.setGroup(studList.get(i).getGroup());
                    studentsToShow.add(a);                                                      //when a new group is chosen the list of students in the selected group needs to be updated
                }                                                                               //this re uses the code earlier to make a list of student in the selected group
            }
            updateSpS();                                                                        //updates the student spinner
        }
        public void onNothingSelected(AdapterView<?> parent){
        }
    });
EN

回答 2

Stack Overflow用户

发布于 2018-07-11 10:03:14

如果您已放置此oncreate事件,则微调器将复制。将微调填充代码放在onResume方法上。

票数 0
EN

Stack Overflow用户

发布于 2018-07-11 10:13:04

从与问题分享的代码片段中,很难猜测为什么OP会有重复值。有根据的猜测是他的onItemSelected()被多次调用。

Spinner的(在我个人看来,是最糟糕的安卓小部件之一) onItemSelected()可以因为不同的原因被调用多次,其中一件事我建议尝试这种方式-

代码语言:javascript
复制
class SpinnerInteractionListener implements AdapterView.OnItemSelectedListener, View.OnTouchListener {

        boolean userSelect = false;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            userSelect = true;
            return false;
        }

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if (userSelect) {
                // Your selection handling code here
                userSelect = false;
                if(view!=null){
                    selectedGroup = groups.get(position);
                    studentsToShow.clear();
                    for(int i = 0; i < studList.size(); i++){
                        if(studList.get(i).getGroup().equals(selectedGroup)){
                            Students a = new Students();
                            a.setFirstName(studList.get(i).getFirstName());
                            a.setLastName(studList.get(i).getLastName());
                            a.setStudentID(studList.get(i).getStudentID());
                            a.setGroup(studList.get(i).getGroup());
                            studentsToShow.add(a);                                                      //when a new group is chosen the list of students in the selected group needs to be updated
                        }                                                                               //this re uses the code earlier to make a list of student in the selected group
                    }
                    updateSpS();                                                                        //updates the student spinner
                }
            }
        }
    }

然后设置-

代码语言:javascript
复制
SpinnerInteractionListener listener = new SpinnerInteractionListener();
spinnerG.setOnTouchListener(listener);
spinnerG.setOnItemSelectedListener(listener);

同时,这会在没有用户接触的情况下处理onItemSelected()的不必要的回调,如果之前有任何泄漏的侦听器的话。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51275407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档