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
Contributor
Posts: 12
Registered: ‎07-11-2013
My Device: Curve 6
My Carrier: mtn
Accepted Solution

Unable To Populate ListView With Vector

I am using a sample i got on the internet to create a listfield

that contains an image and data contained in a vector.

The data is obtained from a JSON Array that is created from a  php  script accessing a mysql database.

 

The issue is that the listfield does not display any content, even when there are elements in the array.

 

This is the code i use to get the JSON Array data:

JSONArray items_array=job.getJSONArray("items"); 
                    for(int i=0;i<items_array.length();i++)
                    {
                        Vector miniholder=new Vector();
                        JSONObject items_obj=items_array.getJSONObject(i);
                        item=new Items();
                        item.setDescription(items_obj.getString("item_description"));
                         item.setId(items_obj.getString("id"));
                         item.setName(items_obj.getString("item_name"));
                         item.setPrice(items_obj.getString("price"));
                         item.setRealPrice(items_obj.getString("real_price"));
                         item.setUrl(items_obj.getString("image_url"));
itemsholder.addElement(item); 

-------
 ls.success(itemsholder); // ls is an instance of the screen

 The code for the  ListFieldCallback :

class MyListModel implements ListFieldCallback
       {
           
           private ListField _view;
           private int _defaultRowHeight=32;
           private int _defaultRowWidth=_defaultRowHeight;
           private int _txtImagePadding=5;
           private Bitmap _bitmap;
            private Vector _data;
           public MyListModel(ListField list,Vector data)
                 {
                     _view=new ListField();
                    _data=new Vector();
                     _data=MyScreen.data_vec;
                     _view=list;
                     list.setCallback(this);
                    // _view.setSize(_data.size());
                     _view.setRowHeight(_defaultRowHeight);
                     _bitmap=null;
                      
                    
                     //list.setSize(_data.size());                     
                     
                     }
           
           
           public void drawListRow(ListField list,Graphics g,int index,int y,int w)
           {
               Items itemToDraw=(Items)this.get(list,index);
               String name=itemToDraw.getName();
               g.drawText(name,_defaultRowWidth+_txtImagePadding,y,DrawStyle.LEADING|DrawStyle.ELLIPSIS,w-_defaultRowWidth-_txtImagePadding);
               
               g.drawBitmap(0,y,_bitmap.getWidth(),_bitmap.getHeight(),_bitmap,0,0);
               
               }

 The code for the success method that should now populate the listview :

public void success(final Vector listboys)
        { 
          
          UiApplication.getApplication().invokeLater(new Runnable()
          {public void run()
          {
             data_vec=new Vector();
          data_vec=listboys;
          Items it1=(Items)data_vec.elementAt(0);
          error("Vector Main Screen " +it1.getName());
                             final MyListField myListView=new MyListField();
      final  MyListModel myListModel=new MyListModel(myListView,data_vec);
     myListView.addToContextMenu(myListModel.getAddMenuItem(0,0));
    myListView.addToContextMenu(myListModel.getRemoveMenuItem(0,0));
    myListView.addToContextMenu(myListModel.getModifyMenuItem(0,0));
    myListView.addToContextMenu(myListModel.getEraseMenuItem(0,0));
    
    {
        Manager vfm=getMainManager();
        vfm.add(myListView);
        vfm.add(new SeparatorField(SeparatorField.LINE_HORIZONTAL));
        setTitle("List Demo Title");
        
        
        }

 The  listview is always empty. I can't see where the data is passed to display. Whenever i call :

list.setSize(_data.size());  

 I always get a null pointer exception. How can i get  the listview to show the data contained in the itemsholder vector. Thanks

 

 

Developer
Posts: 16,750
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Unable To Populate ListView With Vector

data_vec=new Vector(); <-this makes no sense, just work on the listboys vector directly.
In your MyListModel constructor you do not use the parameter data at all, you should.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 12
Registered: ‎07-11-2013
My Device: Curve 6
My Carrier: mtn

Re: Unable To Populate ListView With Vector

I had earlier tried that .....ie i had done :
_data=data (vector passed in constructor)....still no sucess.
Developer
Posts: 16,750
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Unable To Populate ListView With Vector

[ Edited ]

why use _data at all?

 

i would suggest that you use the debugger and see if your assignments do what you expect them to do.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 12
Registered: ‎07-11-2013
My Device: Curve 6
My Carrier: mtn

Re: Unable To Populate ListView With Vector

_data is the vector defined in the class that implements the listfield callback....i dont get what u mean by working with the listboys vector directly.....i believed i had to create a variable in the current class to handle its content......before using this Code....i had done this : public void drawlist(Vector listboys)
{Listboys =new Vector();
Listboys=listboys;
_lfboys=new Listfield(Listboys.size());
_lfboys.setCallback(this);
this.add(_lfboys);
}
and it worked perfectly. However ....with this new code ...any call to setSize() throws a null pointer exception. Where am i missing it
Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Unable To Populate ListView With Vector

IN this circumstance, when completely replacing the data with a new set of data, you could consider creating an entirely new ListField, and replacing the current one with your new one. 

 

I appreciate you have tried to us a MVC paradigm with this Field, but for me, that is problematic and I wouldn't bother - create a ListField extension that creates itself and listens to its own call backs, pass in the data that it is too display, and then add or replace that on the screen.

 

I understand you have looked at loads of ListField samples.  What I have found is that there are loads of variations, all doing things in a slightly different way.  So it is difficult to compare and or combine two different samples.  Putting it all in one place has made this easier for me, even if it is not truly MVC like. 

Contributor
Posts: 12
Registered: ‎07-11-2013
My Device: Curve 6
My Carrier: mtn

Re: Unable To Populate ListView With Vector

I tried to use this sample because of the Provision for images. The sample you gave for a listfield of people worked perfectly. But i will need to include Images in the listview thats why i am using the mvc sample.
Developer
Posts: 16,750
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Unable To Populate ListView With Vector

using images in a listfield is not difficult, enhance the one that worked for you.

you can use drawBitmap to draw the image in the listfield.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 12
Registered: ‎07-11-2013
My Device: Curve 6
My Carrier: mtn

Re: Unable To Populate ListView With Vector

I used drawbitmap...and it gave me some of the images. Other issues have arisen. Which i will discuss in new thread. Thanks.