05-14-2009 09:04 AM
I am pretty new to Blackberry application development, so please excuse any novice questions.
I am developing a simple Java application for Blackberry. Using JDE 4.5, Eclipse 3.4 with Blackberry Plugin and testing using simulator for BB 8300.
My program connects to a web service and returns a list of data which I show on the screen using the ListField from the rim api. But, most of the text is quite long, and as a result the extra text beyond the phone's screen is clipped. I have tried using the VerticalFieldManager and FlowFieldManager, but that doesn't help either.
But if I add as many RichTextStrings as the number of items in the list, those items wrap my text automatically. But I need these items to be selectable, so the list of TextFields doesn't work. And since I only found the ListField as selectable I am using that.
I could not find anything in the documentation or forums to help me with my problem.
Thanks for replies in advance. :-)
05-14-2009 09:13 AM
This thread will give you some clues.
05-14-2009 09:49 AM
Thanks BBDeveloper, for the really quick reply. :-)
I went through the thread pointed out by you and studied the long code snippet pasted there. It was pretty useful and I came to know of a lot of things I did not. The problem there is pretty similar, but my problem is much simpler and I was expecting some Font / Manager setting called Wrap_Text that would do my job. Something similar to the J2ME ListItem's setFitPolicy function which can be set to Choice.TEXT_WRAP_ON.
I would like my screen to look like :
| My very long text here in one item |
| of a ListField wrapped |
| My second item in the List. |
| and so on ....... |
If I use a method similar to the one in the thread, as far as I understand, I would have to break my long string into smaller chunks based on words / width available and then put them in seperate fields and add with different y co-ordinates.
I was wondering if it is possible to use the J2ME MIDP api alongwith the BlackBerry application code that extends UIApplication? Something like a MIDP list inside a screen.
Please suggest if there is any other way to tackle this.
05-14-2009 12:06 PM
There is no such Manager that can do the TEXT_WRAP for you as you mentioned. But your problem is not complicated. Also you do not have to follow the example in the thread. A simpler way to do this is as follows:
in the drawList:
public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width)
1- get the text to be drawn
String  linesToDraw = calculate the number of lines depending on the row width
int ypos = TOP_OFFSET +y;
int fontHeight = this.getFont().getHeight();
for (int i = 0; i < linesToDraw .size(); i++)
textToDraw = (String)linesToDraw.elementAt(i);
graphics.drawText(textToDraw, xpos, ypos);
ypos += fontHeight;
//Draw the separation line
But you still have to have a utility that breaks the text into lines that fit in each row.
This solution is much simpler, since you have to display text only. But you can also add to it bitmaps.
This is just a pseudo code on how to do it, but if you want to adopt this solution and got stuck, I can provide more details.
05-14-2009 12:43 PM
@shishirc - I think you have misread the code that BBDev supplied to you.
Basically instead of you drawing the text yourself (with drawText), with this code you create a Field inside a Manager that draws the contents you wish to display. Then you constrain that Manager to the area you have for your Row and ask it to draw itself.
In your case, you could just put a RichTextField in there, constrain it to the size you have, and then it would do the wrapping for you.,
This is very powerful technique, that is particularly useful when you have stuff like word wrapping or various size components that you want to display. It is also quite complicated, at least for a simple sole like myself. I have only used this in one place.
If all you are displaying is text, then I would go with the approach suggested by rab.
05-14-2009 01:00 PM
That was my thread and I actually ended up writing my own custom class that handled highlighting, scrolling, etc. The main reason for this was that code only allows you to set the row height once: setRowHeight(40), and I had a requirement that had different row heights depending on how much the text wrapped.
If you can settle with fixing the row height I would adapt that code, otherwise you might have to come up with something more complicated.
05-14-2009 01:23 PM - edited 05-14-2009 01:26 PM
No I ended up using a custom VerticalFieldManager rather than a ListField in the end. I didn't want to use the undocumented method since it is undocumented and it also caused some choppiness in the UI.
Here's more discussion on the topic: http://supportforums.blackberry.com/rim/board/mess
05-15-2009 10:41 AM
Thanks for the replies.
These are the things I understood and have implemented:
1) Using rows of manager as suggested by BBDev.
2) Layout of multiple (two currently) field elements in a row as required.
3) Using a RichTextField that wraps data on its own.
4) I have two fields one for the label (heading) and one for the text in the next line.
5) I keep my row height constant for each row of the list.
I want to correct points 4,5 as follows:
My queries and problems:
1) @bo: I would like to know how I can customize height for each row in the list.
2) @rab/peter: I have one text which is the label for the text that follows. I want the label and the field to have different fonts. So my text should be like:
<b>Label Name:</b> My Field text which is wrapped
and onto the next line, but it should appear below the
label name as well.
Text between <b> denotes different font style.
While I could create Label as a sepearate feild and Field text as another field, I was not able to align it such that, the overflow of the field text starts from the 0 index of the next line. This is important since we are targetting smaller screen fones and space is expensive.
Another query, I started working on JDE 4.5, but the requirement now is for JDE4.2 (BB pearl models). I was using a web service to retrieve data that I show, but since 4.2 does not have the WS api, which conversion should be easiest for me?
I have started working on the BB platform for a little over 2 weeks, Please excuse any stupid questions. :-)
05-15-2009 11:33 AM
"I have started working on the BB platform for a little over 2 weeks, Please excuse any stupid questions. :-) "
You have made great progress....
"4) I have two fields one for the label (heading) and one for the text in the next line. "
As you are only doing this to get a different font on the Label, instead have a look at just using RichTextField and the following KB Article:
How To - Format text in a RichTextField
Article Number: DB-00124
If you are using true Web Services, then look at using kSoap in your 4.2 Build. Search the forum for references. From memory simon_hain uses kSoap in all his apps in preference to the built-in code - not sure why but it might be because that way, he has one code base and so it is much easier to debug.