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
SivaR
Posts: 75
Registered: ‎02-08-2010
My Device: 9000
My Carrier: Airtel
Accepted Solution

How to implement ListFieldCallback

Hi

I am new to Blackberry development environment and I am trying to develop an App containing one screen with List of Text items (List element). I am reading lot of development guides, I am clueless in how to implement ListCallback.

Can one help me regarding the same (like private class ListCallback implements ListFieldCallback )

 

Thanks & Regards

Siva

Thanks
Siva
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to implement ListFieldCallback

Hi and Welcome to the forums.

 

You will find as a new BlackBerry developer, that a lot of your questions will probably have been already asked.  So do a search of the forum.  It is not that easy to search, so try a number of different keywords if you can. 

 

In this case, if you had searched for "sample listfield", you probably would have found this Thread as the first link.

http://supportforums.blackberry.com/t5/Java-Development/Simple-ListField-example/td-p/426809

 

There are others there as well, but I do think this one will help you.

 

Hope it does and hope your enjoy you BlackBerry development experience.

Please use plain text.
Developer
SivaR
Posts: 75
Registered: ‎02-08-2010
My Device: 9000
My Carrier: Airtel

Re: How to implement ListFieldCallback

Thanks. It certainly helped me.

 

I found that Graphics.getScreenWidth() API is deprecated in Latest version. You have used this your ListCallBack class as follows

 

   public int getPreferredWidth(ListField list) {
      return Graphics.getScreenWidth();
   }

 

I found some one is using Dialog.getWidth(); but that is also not there.

 

Can you help

 

Thanks

Siva

Thanks
Siva
Please use plain text.
Developer
SivaR
Posts: 75
Registered: ‎02-08-2010
My Device: 9000
My Carrier: Airtel

Re: How to implement ListFieldCallback

[ Edited ]

I found the below code is working for me (even with Deprecated APIs)

 

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

public class cList extends UiApplication
{
    private ListField myList;

    public static void main(String[] args)
    {
        cList app = new cList();
        app.enterEventDispatcher();
    }

    private static class ListCallback implements ListFieldCallback
    {
        private Vector listElements = new Vector();

        public void drawListRow(ListField list, Graphics g, int index, int y, int w)
        {
            String text = (String)listElements.elementAt(index);
            g.drawText(text, 0, y, 0, w);
        }
        public Object get(ListField list, int index)
        {
            return listElements.elementAt(index);
        }
        public int indexOfList(ListField list, String p, int s)
        {
            return listElements.indexOf(p, s);
        }
        public int getPreferredWidth(ListField list)
        {
            return Graphics.getScreenWidth();
        }
        public void insert(String toInsert, int index)
        {
            listElements.addElement(toInsert);
        }
        public void erase()
        {
            listElements.removeAllElements();
        }
    }

    public cList()
    {
        MainScreen mainScreen = new MainScreen();
        myList = new ListField();
        ListCallback myCallback = new ListCallback();
        myList.setCallback(myCallback);

        String fieldOne = "ListField one";
        String fieldTwo = "ListField two";
        String fieldThree = "ListField three";

        myList.insert(0);
        myCallback.insert(fieldOne, 0);

        myList.insert(1);
        myCallback.insert(fieldTwo, 1);

        myList.insert(2);
        myCallback.insert(fieldThree, 2);

        mainScreen.add(myList);
        mainScreen.setTitle(": This is Heading of List :");
        pushScreen(mainScreen);
    }
}

 

Thanks
Siva
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to implement ListFieldCallback

[ Edited ]

You are right, the

 

Graphics.getScreenWidth();


should be replaced with

 

Display.getWidth();

 

Glad you got it going.

 

BTW, did you read right to the end of the Thread.  There is an example I put at the end that uses ListField in a different (and I think easier to understand) way.

Please use plain text.
Developer
SivaR
Posts: 75
Registered: ‎02-08-2010
My Device: 9000
My Carrier: Airtel

Re: How to implement ListFieldCallback

Yes, I got what you are referring and I am able to run with that code also. Thanks for the same.

 

This triggered me two more questions

 

Question 1:

How to display a list with an ICON, Name and a small Icon.

If I compare single Line of a List to a printf, I want to have a class which accepts variable parameters and types as follows

 

printf("%LICON %s %SICON",1.png, "hello", 2.png);

 

LICON and SICON are different differentiators.

 

Question 2:

How to send List data from one Application/Thread to another Application/Thread respectively.

 

Thanks

Siva

Thanks
Siva
Please use plain text.
Developer
peter_strange
Posts: 19,599
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to implement ListFieldCallback

1. This is relatively easy, but you have to do the formatting yourself.  So if you have an icon, your use grpahics.drawbitmap in your drawListRow method to draw this, then start your drawText a bit further along so it doesn't draw over the icon. 

 

2. Are these programs in the same application?  If so, then use the standard Java mechanisms.  If not, then search the forum for IPC (Inter Process Communication), as this question has been asked before. 

Please use plain text.
Developer
SivaR
Posts: 75
Registered: ‎02-08-2010
My Device: 9000
My Carrier: Airtel

Re: How to implement ListFieldCallback

Thanks peter (hope I can call you as peter) for your quick response and help

 

Thanks & Regards

Siva

Thanks
Siva
Please use plain text.