【问题标题】:Keywordfilter field in blackberry黑莓中的关键字过滤字段
【发布时间】:2011-12-30 19:45:38
【问题描述】:

我对从 db 填充的列表使用了关键字过滤器。它工作得很好。想象一下该列表包含一个短语“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();}

            }




        }        
    }       
}

【问题讨论】:

    标签: blackberry


    【解决方案1】:

    此查询对您有用:

    SELECT Name FROM Employee where Name like '%d%' order by Name;

    这里名称是我的列名;

    试试这个;

    【讨论】:

      【解决方案2】:

      我认为您正在使用如下查询

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

      这里你会得到所有包含字母“b”的名字(区分大小写) 如果您想要所有以字母“b”开头的单词,请使用类似

      的 writr 查询
        select colomname from tablename where colomname GLOB 'b%' order by colomname ;
      

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

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多