黑莓中的Keywordfilter字段

时间:2011-12-26 07:16:59

标签: blackberry

我使用了一个关键字过滤器来填充从db .Its工作正常的列表。想象一下,列表中包含一个短语'A big bat',还有其他单词以'b'开头,就像书籍等。但是,当我给' b'在搜索字段中首先出现短语'A big bat',然后只显示以'b'开头的单词。请帮我修复它 请看我的代码

public final class KeywordFilter 
{
    private KeywordFilterField _keywordFilterField;    
    private WordList _wordList;
    private Vector _words;  


    public KeywordFilter()
    {      

        _words = getDataFromDatabase();

        if(_words != null)
        {

            _wordList = new WordList(_words);        


            _keywordFilterField = new KeywordFilterField();                   
            _keywordFilterField.setSourceList(_wordList, _wordList);      


            CustomKeywordField customSearchField = new CustomKeywordField();
            _keywordFilterField.setKeywordField(customSearchField);                      


            KeywordFilterScreen screen = new KeywordFilterScreen(this);


            screen.setTitle(_keywordFilterField.getKeywordField());            


            screen.add(_keywordFilterField);
             UiApplication ui = UiApplication.getUiApplication();

           ui.pushScreen(screen);

        } 
        else
        {
            UiApplication.getUiApplication().invokeLater(new Runnable()
            {
                public void run()
                {
                    Dialog.alert("Error reading data file.");
                    System.exit(0);
                }
            });
        }       
    }   


    KeywordFilterField getKeywordFilterField()
    {
        return _keywordFilterField;
    }   


    private Vector getDataFromDatabase()
    {   
        Vector words = new Vector();
        Database d;
            for(;;)
            {
                try
                {
                    URI myURI=URI.create("file:///SDCard/Databases/MyTestDatabase.db");
                    d=DatabaseFactory.open(myURI);
                   Statement st=d.createStatement("select (select distinct group_concat(eng) fromEnglish),group_concat(mal) from English e ,Malayalam m where e.Ecode=m.Mcode group by eng");
                   st.prepare();
                   net.rim.device.api.database.Cursor c=st.getCursor();
                   Row r;

                   while(c.next())
                   {

                    r=c.getRow();
                    String w=r.getString(0);
                    String meaning=r.getString(1);

                    words.addElement(new Word(w,meaning));
                   }
                   st.close();
                   d.close();
                   }
                     catch ( Exception e ) 
        {         
            System.out.println( e.getMessage() );
            e.printStackTrace();
        }                      


            return words;           
        }

    } 


    void addElementToList(Word w)
    {       
         _wordList.addElement(w);
        _keywordFilterField.updateList();       
    }   


    final static class CustomKeywordField extends BasicEditField
    {   

        CustomKeywordField()
        {

            super(USE_ALL_WIDTH|NON_FOCUSABLE|NO_LEARNING|NO_NEWLINE); 

            setLabel("Search: ");
        } 


        protected boolean keyChar(char ch, int status, int time)
        {
            switch(ch)
            {
                case Characters.ESCAPE:

                    if(super.getTextLength() > 0)
                    {
                        setText("");                        
                        return true;
                    }

            }                
            return super.keyChar(ch, status, time);
        }                     


        protected void paint(Graphics graphics)
        {            
            super.paint(graphics);


            getFocusRect(new XYRect());
            drawFocus(graphics, true);                          
        }
    }
}



  class KeywordFilterScreen extends MainScreen
{
    private KeywordFilter _app;
    private KeywordFilterField _keywordFilterField;    

    public KeywordFilterScreen(KeywordFilter app)
    {

        _app = app;


        _keywordFilterField = _app.getKeywordFilterField();



    }


    protected boolean keyChar(char key, int status, int time)
     {
         if (key == Characters.ENTER)
        {
           displayInfoScreen();
             // Word w = (Word)_keywordFilterField.getSelectedElement(); 
             // Status.show(w.getMeaning()); 

            return true; 
        }             
        return super.keyChar(key, status, time);
    }    


    public boolean invokeAction(int action)
    {        
        switch(action)
        {
            case ACTION_INVOKE: 

                displayInfoScreen();   

                return true; 
        }    
        return  super.invokeAction(action);
    }


    private void displayInfoScreen()
    {

        Word w = (Word)_keywordFilterField.getSelectedElement(); 
        if(w != null)
        {            
            InfoScreen infoScreen = new InfoScreen(w);
            UiApplication ui=UiApplication.getUiApplication();
            ui.pushScreen(infoScreen);


        }
    }


    public boolean onSavePrompt()
    {
        return true;
    }



    private final static class InfoScreen extends MainScreen
    {

        InfoScreen(Word w)
        {  

            setTitle(w.toString());            
            BasicEditField popField = new BasicEditField(" ",w.getMeaning(),300,Field.NON_FOCUSABLE);

          FontManager.getInstance().load("DC124.TTF", "MyFont", FontManager.APPLICATION_FONT) ;
        {
             try {
                FontFamily typeface = FontFamily.forName("MyFont");
                Font myFont = typeface.getFont(Font.PLAIN, 25);
                popField.setFont(myFont);
                add(popField);
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();}

            }




        }        
    }       
}

2 个答案:

答案 0 :(得分:0)

我认为你正在使用如下的查询

select colomname from tablename where colomname GLOB '%b%';

这里你得到所有包含字母'b'的名字(区分大小写) 如果你想要所有用字母'b'表示的话,那就应该像

一样写
  select colomname from tablename where colomname GLOB 'b%' order by colomname ;

这里我使用的是“GLOB”,如果您不需要区分大小写,那么它会对输出记录区分大小写,那么您可以使用“LIKE”关键字

谢谢

答案 1 :(得分:0)

此查询对您有用:

  

SELECT Name FROM Employee,其中名称如'%d%'按名称排序;

此处名称是我的列名;

试试这个;