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
Developer
Posts: 88
Registered: ‎02-11-2009
My Device: Not Specified

ListField Storage

HI All,

In my application there is ListField with Ten rows.i add element to listfield at particular index at runtime .i want to store that element in listField.So that used that value at later time.How i store particular value at particular index at runtime in listfield.And also remove element from Storages.

Thanks

Developer
Posts: 19,628
Registered: ‎07-14-2008
My Device: Not Specified

Re: ListField Storage

Your question suggests that you have not understood how ListField works. 

 

Please review the ListField samples that you will find around, you will find some in the KB that will be helpful.  Once you have reviewed these, I think you question will be answered, but if it is not, perhaps you can explain your question again, suing one of the samples to illustrate your points. 

Developer
Posts: 88
Registered: ‎02-11-2009
My Device: Not Specified

Re: ListField Storage

hi peter

i hav used this code i want to store number at particular index.when i store that number to that index then it persist but it no do that how i store number at particular index.

 

 

final class demoScreen extends MainScreen 
{
    
  
   public demoScreen () 
   {
       
        final MyListField myListView = new MyListField();
        final MyListModel myListModel = new MyListModel(myListView);
        
        System.out.println("myListModel " +myListModel);
        System.out.println("myListView "+myListView);
        
        myListView.addToContextMenu(myListModel.getAddMenuItem(0, 0));
        myListView.addToContextMenu(myListModel.getRemoveMenuItem(0, 0));
        {
            Manager vfm = getMainManager();
            vfm.add(myListView);
            setTitle(new LabelField("demo Number"));
        }
        
        myListView.insert(0);
        myListModel.insert("",0);
        
        myListView.insert(1);
        myListModel.insert("",1);
        
        myListView.insert(2);
        myListModel.insert("",2);
        
        myListView.insert(3);
        myListModel.insert("",3);
        
        myListView.insert(4);
        myListModel.insert("",4);
        
        myListView.insert(5);
        myListModel.insert("",5);
        
        myListView.insert(6);
        myListModel.insert("",6);
        
        myListView.insert(7);
        myListModel.insert("",7);
        
        myListView.insert(8);
        myListModel.insert("",8);
        
        myListView.insert(9);
        myListModel.insert("",9);
        
        //Remove Application Menu if already Added
        removeApplicationMenu();

        //Method to add menu to Default application.
        addApplicationMenu();
      
   
      
    
   }
   
 
class MyListField extends ListField 
{

    private Menu _contextMenu = new Menu();
    public void addToContextMenu(MenuItem menuitem) 
    {
        if (menuitem != null) _contextMenu.add(menuitem);
    }

     public ContextMenu getContextMenu() 
     {
            ContextMenu cMenu = super.getContextMenu();
            cMenu.clear();
            int size = _contextMenu.getSize();
            for (int i = 0; i < size; i++) 
            {
                cMenu.addItem(_contextMenu.getItem(i));
            }
        return cMenu;
     }

}

interface ActionListener 
{
    void actionPerformed(Object source);
}

class MyListModel implements ListFieldCallback 
{

        private static Vector _data = new Vector();
        private ListField _view;
        private int _defaultRowHeight = 32;
        private int _defaultRowWidth = _defaultRowHeight;
        private int _textImagePadding = 5;
        private Bitmap _bitmap;
        
        private static final long ID = 0xb06b0360513341f1L;
        private static PersistentObject _store;
  
  
 
  

        public MyListModel(ListField list) 
        {
            _view = list;
            list.setCallback(this);
            _view.setRowHeight(_defaultRowHeight);
    
    
                if(null == _store ) 
                {
                    _store = PersistentStore.getPersistentObject(ID);
                }
                readList();
    
    
        }

 
        public void drawListRow(ListField list, Graphics g, int index, int y, int w) 
        {
      
       
                String text = (String) _data.elementAt(index);
                g.drawText(text, _defaultRowWidth + _textImagePadding, y,DrawStyle.LEADING | DrawStyle.ELLIPSIS,w - _defaultRowWidth - _textImagePadding);
                if(index == 0)
                {
                    _bitmap = Bitmap.getBitmapResource( "demo.png" );
                    g.drawBitmap(0, y, _bitmap.getWidth(), _bitmap.getHeight(), _bitmap, 0, 0);
                }
                else if(index == 1)
                {
                    _bitmap = Bitmap.getBitmapResource( "demo1.png" ); 
                    g.drawBitmap(0, y, _bitmap.getWidth(), _bitmap.getHeight(), _bitmap, 0, 0);
     
                }
                else if(index == 2)
                {
                    _bitmap = Bitmap.getBitmapResource( "demo2.png" );
                    g.drawBitmap(0, y, _bitmap.getWidth(), _bitmap.getHeight(), _bitmap, 0, 0);
     
                }
                else if(index == 3)
                {
                    _bitmap = Bitmap.getBitmapResource( "demo3.png" );
                    g.drawBitmap(0, y, _bitmap.getWidth(), _bitmap.getHeight(), _bitmap, 0, 0);
                 
                }
                else if(index == 4 )
                {
                    _bitmap = Bitmap.getBitmapResource( "demo4.png" );  
                    g.drawBitmap(0, y, _bitmap.getWidth(), _bitmap.getHeight(), _bitmap, 0, 0);
     
                }   
                else if(index == 5 )
                {
                    _bitmap = Bitmap.getBitmapResource( "demo4.png" );  
                    g.drawBitmap(0, y, _bitmap.getWidth(), _bitmap.getHeight(), _bitmap, 0, 0);
     
                }    
                else if(index == 6 )
                {
                    _bitmap = Bitmap.getBitmapResource( "demo4.png" );  
                    g.drawBitmap(0, y, _bitmap.getWidth(), _bitmap.getHeight(), _bitmap, 0, 0);
     
                }    
             
  }

  public Object get(ListField list, int index) 
  {
        return _data.elementAt(index);
  }

  public int indexOfList(ListField list, String p, int s) 
  {
        return _data.indexOf(p, s);
  }

  public int getPreferredWidth(ListField list) 
  {
        return Display.getWidth();
  }

  public void insert(String toInsert, int index) 
  {
       _data.insertElementAt(toInsert,index); 
        
  }

  public void delete(int index) 
  {
        _data.removeElementAt(index);
    
  }
 
  public int size() 
  {
        return _data.size();
  }
  
    
  public ActionListener getAddAction() 
  {
        return new ActionListener() 
        {
            public void actionPerformed(Object source) 
            {
                int index = _view.getSelectedIndex();
                insert("123456455", index);
                if( null!= _data ) 
                {
                    synchronized( _store ) 
                    {
                        _store.commit();
                    }
                }
            }
        };
  }

  public ActionListener getRemoveAction() 
  {
      
        final int index = _view.getSelectedIndex();  
        return new ActionListener() 
        {
            public void actionPerformed(Object source) 
            {
                delete(index);
                if( null!= _data ) 
                {
                    synchronized( _store ) 
                    {
                        _store.commit();
                    }
                }
            }
        };
  }

  
  public MenuItem getAddMenuItem(int ordinal, int priority) 
  {
        return new MenuItem("Add", ordinal, priority) 
        {
                public void run() 
                {
                    getAddAction().actionPerformed(_view);
                }
        };
  }

  public MenuItem getRemoveMenuItem(int ordinal, int priority) 
  {
        return new MenuItem("Remove", ordinal, priority) {
      public void run() {
        getRemoveAction().actionPerformed(_view);
      }
    };
  }
  
    /**
     * Initialize or reload our persistent store.
     */
   /*  
    static
    {
        _store = PersistentStore.getPersistentObject(ID);
        
        if(_store.getContents()==null)
        {
            _data= new Vector();
            _store.setContents(_data);
        }
        
        _data=(Vector)_store.getContents();
    }
 
  
    private static void commit()
    {
        _store.setContents(_data);
        _store.commit();
    } */   
    
     public void readList() {
        System.out.println( "readList" );
        synchronized( _store ) {
            if( _store != null ) {
                _data = ( Vector ) _store.getContents();
                if ( null == _data ) {
                    _data = new Vector();
                    _store.setContents( _data );
                }
            }
        }
    }
 
}