在黑莓手机中找不到数据库列表后退按钮

时间:2011-12-09 10:57:17

标签: blackberry

在screen1中我有2个按钮a和bi已经给出了两个字段更改监听器,每个按下相应的屏幕。当我按下按钮'a'时,它按下屏幕2,我用了一个keywordFilter从sqlite数据库中搜索单词当我运行应用程序时,所有这些都是完美的,但是当我按下后面(或上一个键),然后再次按下屏幕1中的按钮'a'时,我没有找到来自database.same的任何结果按钮b也发生了.pblm请帮忙解决什么问题。 非常感谢你 下面是单击按钮“a”后调用的类

import net.rim.device.api.ui.*;
import net.rim.device.api.io.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.system.*;
import java.util.*;

import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseFactory;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;


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 eng,mal FROM Malayalam m,English e where e.Ecode=m.Mcode");
                   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));
                   }}
                     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);                          
        }
    }
}
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

 final 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();
            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);
             ui.popScreen(this);

        }
    }




    private final static class InfoScreen extends MainScreen
    {

        InfoScreen(Word w)
        {  

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




        }        
    }       
}
public class Word
{
    private String _word;
    private String _meaning;



    public Word(String word, String meaning)
    {
        _word = word;
        _meaning = meaning;

    }

    String getMeaning()
    {
        return _meaning;
    }




    public String toString()
    {
        return _word;
    }
}
import net.rim.device.api.ui.component .*;
import net.rim.device.api.collection.util.*; 
import net.rim.device.api.util.*;
import java.util.*;

public class WordList extends SortedReadableList implements KeywordProvider
{

    public WordList(Vector words)
    {
        super(new WordListComparator());    

        loadFrom(words.elements());      
    } 


    void addElement(Object element)
    {
        doAdd(element);        
    }    

    public String[] getKeywords( Object element )
    {        
        if(element instanceof Word )
        {            
            return StringUtilities.stringToWords(element.toString());
        }        
        return null;
    }  


    final static class WordListComparator implements Comparator
    {   

        public int compare(Object o1, Object o2)
        {
            if (o1 == null || o2 == null)
              throw new IllegalArgumentException("Cannot compare null words");

            return o1.toString().compareTo(o2.toString());
        }        
    }    
}

1 个答案:

答案 0 :(得分:1)

添加st.close();在关闭try块之前访问私有Vector getDataFromDatabase()方法中的db之后的d.close()