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
Highlighted
New Developer
Posts: 14
Registered: ‎01-07-2009
My Device: Not Specified
Accepted Solution

Scrolling AND selecting items in a ListField on the Storm?

[ Edited ]

Background info: 

BlackBerry Storm 9500

BlackBerry JDE 4.7.0

 

Problem:

I am using a ListField to display multiple items in a list. I am also using the touchEvent(TouchEvent message) method to listen for a click (TouchEvent.CLICK). Everything seems to work just fine unless I need to scroll the list. It seems that by using the touchEvent method, I cannot scroll the list. If I remove the touchEvent method, scrolling works but I am unable to use the touch interface to select an item in the list by clicking on it.

 

Question:

Is there a way to get both working? That is, can I enable scrolling in the ListField AND allow the user to select an item by clicking on it?

 

Code:

     private class SlideList extends MainScreen

     {

          // Fields

 

          private ListField _list;

          private ListCallback _callback;

          

 

          // Constructor

          

          public SlideList()

          {

               super();

 

 

             //add a screen title

             LabelField title = new LabelField(getTitle(), LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);

             setTitle(title);

             

             setupList();

          }

 

          private void setupList()

          {

              if (_list != null)

                   delete(_list);

              

               // Get all slides and put their titles and subtitles into an array

               String[] slideTitles = getSlideTitles();

 

               

              _list = new ListField();

              _callback = new ListCallback();

              _list.setCallback(_callback);

 

              // Build exercise list.

             for (int index=0; index<slideTitles.length; index++)

             {

                 _list.insert(index);

                 _callback.insert(slideTitles[index], index);

             }

             _list.setSelectedIndex(currentSlideNum);

             add(_list);

          }

 

          

          // Get touch input.

          

          protected boolean touchEvent(TouchEvent message)

          {

               if (message.getEvent() == TouchEvent.CLICK)

               {

                    // Check if a list item is selected.

                    if (_list.getSelectedIndex() > -1)

                    {

//                         Dialog.alert("You selected: "+_list.getSelectedIndex());

                         itemSelected(_list.getSelectedIndex());

                         return true;

                    }

                    else

                         return false;

               }

               else

                    return false;

          }

          

          private void itemSelected(int listIndex)

          {

               currentSlideNum = listIndex;

               displaySlide();

               UiApplication.getUiApplication().popScreen(this);     // Remove this screen to display the program.

          }

 

          private 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, slideIcons[index].getWidth()+10, 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.getSelectedIndex();

                     return listElements.indexOf(p, s);  

                  }

                     

                  public int getPreferredWidth(ListField list) {  

                       Screen sr = UiApplication.getUiApplication().getActiveScreen();

                       return sr.getWidth();

                  }

                     

                  public void insert(String toInsert, int index) {  

                     listElements.insertElementAt(toInsert, index);  

                  }

                     

                  public void erase() {  

                     listElements.removeAllElements();  

                  }

          }

 

     }

 

 

Message Edited by reyrj on 01-07-2009 12:04 PM
BlackBerry Development Advisor
Posts: 15,698
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Scrolling AND selecting items in a ListField on the Storm?

You can differentiate between a user touching the screen and pressing/clicking the screen by checking TouchEvent.CLICK and TouchEvent.DOWN.  This could allow you to have different behaviour for each (select versus click).
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
New Developer
Posts: 14
Registered: ‎01-07-2009
My Device: Not Specified

Re: Scrolling AND selecting items in a ListField on the Storm?

Mark,

 

Thanks for your reply. It turns out the solution I was looking for was very simple. Rather than returning

false

in the touchEvent method, I needed to return

super.touchEvent(message)

so that touchEvent can call its parent class and still listen for other events inherent to a listenField, such as scrolling with your finger.

 

It now looks like this:

 

          // Get touch input.

          

          protected boolean touchEvent(TouchEvent message)

          {

               if (message.getEvent() == TouchEvent.CLICK)

               {

                    // Check if a list item is selected.

                    if (_list.getSelectedIndex() > -1)

                    {

//                         Dialog.alert("You selected: "+_list.getSelectedIndex());

                         itemSelected(_list.getSelectedIndex());

                         return true;

                    }

               }

               return super.touchEvent(message);

          }