Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
ocinisme
Posts: 2
Registered: ‎01-31-2012
My Device: 9780
My Carrier: DiGi

Accessing SQLiteDemoScreen from another Screen

[ Edited ]

Hi,

 

I am trying to access the SQLiteDemoScreen (the build in sample project from RIM) from a button that I created in my application's main screen.

 

The problem is when I clicked on the button, nothing happened. Eventhough I already apply to pushScreen function. Below is my code.

 

As for information, the rest of the code I copied 100% the same from the sample with very minor modification like classes name and variable, others than that is remain untouched.

 

This is the fragment of the code, this one is from the MyAppScreen.java

 

 

protected void showSchedulerScreen() 
	{
		SQLiteDemoScreen screen = new SQLiteDemoScreen (new SQLManager(_db));
		UiApplication.getUiApplication().pushScreen(screen);
		this.setDirty(false);	
	}

 

And this one is the SQLiteDemoScreen

 

public final class SQLiteDemoScreen extends MainScreen implements TreeFieldCallback    
{   
    private TreeField _treeField;        
    private IntHashtable _categoriesHashtable;
    private Vector _directoryItems;
    private SQLManager _sqlManager;     
    
    
    /**
     * Constructs a new SQLiteDemoScreen
     * @param sqlManager A sqlManager instance used to perform database operations
     */    
    public SQLiteDemoScreen(SQLManager sqlManager)    
    {
        super();     
        _sqlManager = sqlManager;       
        
        // Initialize UI components
        setTitle("SQLite Demo");
        _treeField = new TreeField(this, Field.FOCUSABLE);       
        _treeField.setDefaultExpanded( false );
        add( _treeField );      
        
        // Populate the tree field and vectors with categories and items
        populateCategories();        
        populateItems();                   
    } 
    
    
    /**
     * Obtains the category hash table from the SQLManager and adds a node
     * for each category to the tree field.
     */
    private void populateCategories()
    {        
        _categoriesHashtable = _sqlManager.getCategories();    
        
        IntEnumeration enumeration = _categoriesHashtable.keys();        
        
        int key;
        Category category;
        int categoryNode;
        while(enumeration.hasMoreElements())
        {
            key = enumeration.nextElement();
            category = (Category)_categoriesHashtable.get(key);                                
            categoryNode = _treeField.addChildNode(0, category);                
            category.setNode(categoryNode);              
        }       
    }
    
    /**
     * Obtains the DirectoryItems vector and adds a node for each item to the
     * respective category node in the tree field.
     */
    private void populateItems()
    {   
        _directoryItems = _sqlManager.getItems();   
        
        DirectoryItem directoryItem;
        Category category;
        int categoryNode;
        int itemNode;
        
        // For each directory item, obtain the corresponding Category object from
        // the hash table, then add a child node to the respective category node
        // in the tree field.
        for(int i = _directoryItems.size() - 1; i >= 0; --i)
        {
            directoryItem = (DirectoryItem)_directoryItems.elementAt(i); 
            category = (Category)_categoriesHashtable.get(directoryItem.getCategoryId());
            categoryNode = category.getNode();
            itemNode = _treeField.addChildNode(categoryNode, directoryItem.getName()); 
            directoryItem.setNode(itemNode);                                                              
        }                  
    } 
     
     
    /**
     * Pushes a modal screen to display an existing directory item's details
     * @param currentNode The currently highlighted node in the tree field
     */       
    private void displayItem(int currentNode)
    {     
        DirectoryItem item;
        
        // Loop through the DirectoryItems vector until the object is found which
        // corresponds to the currently highlighted node.
        for(int i = _directoryItems.size() -1; i >= 0; --i)
        {
            item = (DirectoryItem)_directoryItems.elementAt(i);
            if(item.getNode() == currentNode)
            {
                DirectoryItem itemCopy = new DirectoryItem(item);
                            
                UiApplication app = UiApplication.getUiApplication();
                app.pushModalScreen(new ItemScreen(item, _sqlManager, false));      
                
                // Check whether the item was changed by the user
                if(!itemCopy.equals(item))                
                {
                    _treeField.setCookie(currentNode, item.getName());  // Item was edited, update the cookie for the current node                   
                }
                break;            
            }
        }       
    }
       
    /**
     * @see Screen#keyChar(char, int, int)      
     */
    protected boolean keyChar(char key, int status, int time)
    {
         // Intercept the ENTER key. 
        if (key == Characters.ENTER)
        {
            int currentNode = _treeField.getCurrentNode();
        
            if(_treeField.getCookie(currentNode) instanceof String)
            {
                displayItem(currentNode);  
            }
            return true;          
        }   
        
        return super.keyChar(key, status, time);
    }
    
    
    /**
     * @see net.rim.device.api.ui.Screen#invokeAction(int)
     */
    protected boolean invokeAction(int action)
    {        
        if(action == ACTION_INVOKE) 
        {
            int currentNode = _treeField.getCurrentNode();
    
            if(_treeField.getCookie(currentNode) instanceof String)
            {
                displayItem(currentNode);  
            }                
            return true; 
        }    
        
        return  super.invokeAction(action);
    }  
    
    /**
     * @see Screen#onClose()
     */
    public boolean onClose()
    {      
        _sqlManager.closeDB();
        
        return super.onClose();
    }
    
    /**
     * @see TreeFieldCallback#drawTreeItem(TreeField, Graphics, int, int, int, int)      
     */
    public void drawTreeItem(TreeField treeField, Graphics graphics, int node, int y, int width, int indent) 
    {
        if( treeField == _treeField ) 
        {        
            Object cookie = _treeField.getCookie(node);           
                    
            if(cookie instanceof String) 
            {
                String text = (String)cookie;
                graphics.drawText(text, indent, y, Graphics.ELLIPSIS, width);
            }
            
            if(cookie instanceof Category) 
            {
                Category category = (Category)cookie;
                String text = category.getName();
                graphics.drawText(text, indent, y, Graphics.ELLIPSIS, width);
            }
        }        
    }        
...
}

 

Please help me solve this issue, I am still learning the inside-out of Blackberry.


Please use plain text.
Administrator
MSohm
Posts: 14,313
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Accessing SQLiteDemoScreen from another Screen

Is any error or exception thrown when you create the new instance of SQLiteDemoScreen?  Are you testing on a real device or in the simulator?  Comment out the two populate methods and see if the screen displays with an empty tree.

 

Which BlackBerry Smartphone model and BlackBerry handheld software version are you testing on?  You can find this under Options, About on the BlackBerry Smartphone.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
New Contributor
ocinisme
Posts: 2
Registered: ‎01-31-2012
My Device: 9780
My Carrier: DiGi

Re: Accessing SQLiteDemoScreen from another Screen

[ Edited ]

I tried to comment out the 2 methods you mentioned above and it enables me to move to the treefield screen. As you said, it will display an empty treefield.

But I tried to add a new category, and it did not work, it gave me a NullPointerException. And when I wanted to click back to navigate back to my mainscreen, it gave me a NullPointerException as well.

 

FYI I am trying it on Eclipse through the 9800 Simulator.

Please use plain text.
Administrator
MSohm
Posts: 14,313
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Accessing SQLiteDemoScreen from another Screen

[ Edited ]

Set some break points in those methods and step through them to see which line of code is throwing the exception.  That'll show you what's null (and be some good getting started practice with the tools). :smileyhappy:

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.