日期:2014-05-20  浏览次数:20902 次

制作简单的搜索引擎
利用swt的Combo下拉菜单控件制作一个简单的类似百度的搜索引擎,假如当在combo的文本框中输入a时,能够读取到数据库中数据a,ab,abc,abcd然后填充到combo的下拉菜单中;当输入ab时,下拉菜单为ab,abc,abcd;当输入abc时,下拉菜单为abc,abcd。就是在文本框文本值更改事件方法中写入:
Java code

Searchcombo.addListener(SWT.Modify, new Listener() {
            
            @Override
            public void handleEvent(Event arg0) {
                String value=Searchcombo.getText().trim().toLowerCase();
                if (Searchcombo.getItemCount()!=0) {
                    Searchcombo.removeAll();
                }
                //去读数据库
                List<String> keys=getsss();
                for (String key : keys) {
                    if (key.indexOf(value)!=-1) {
                        Searchcombo.add(key);
                        System.out.println(Searchcombo.getItemCount());
                    }
                }
            }
        });



这样写就会出错,因为Searchcombo.removeAll();方法默认更改文本框值,就会重新调用此方法,从而死循环.所以我更改为:
Java code

Searchcombo.addListener(SWT.Modify, new Listener() {
            
            @Override
            public void handleEvent(Event arg0) {
                String value=Searchcombo.getText().trim().toLowerCase();
                if (Searchcombo.getItemCount()!=0) {
                    String[] removeItem=Searchcombo.getItems();
                    //循环删除下拉菜单选项
                    for (int i = 0; i < removeItem.length; i++) {
                        System.out.println(removeItem[i]+"--");
                        Searchcombo.remove(removeItem[i]);
                    }
                    Searchcombo.setText(value);
                }
                
                List<String> keys=getsss();
                for (String key : keys) {
                    if (key.indexOf(value)!=-1) {
                        Searchcombo.add(key);
                        System.out.println(Searchcombo.getItemCount());
                    }
                }
            }
        });



可是这么写下拉菜单项总是出现重复项,就是有N多个a,N多个ab,n多个abc等等,debug跟踪也总是跟踪不到,请高手更改一下我的代码,那里写错了,谢谢

------解决方案--------------------
关键①是不能删除掉选定项②不能重复添加项
代码如下
Java code

            public void handleEvent(Event arg0) {
                String value = searchCombo.getText().trim().toLowerCase();
                String[] items = searchCombo.getItems();
                int selection = searchCombo.getSelectionIndex();
                String selectText;
                if (selection==-1) selectText = null;
                else selectText = searchCombo.getItem(searchCombo.getSelectionIndex());
                for (int i=0; i<items.length; i++) {
                    if (selection!=i) searchCombo.remove(items[i]);
                }
                // 去读数据库
                List<String> keys = getsss();
                for (String key : keys) {
                    if (key.indexOf(value) != -1 && !key.equals(selectText)) { 
                        searchCombo.add(key);
                        System.out.println(searchCombo.getItemCount());
                    }
                }