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: 121
Registered: ‎05-31-2012
My Device: 9900
My Carrier: Airtel
Accepted Solution

LabelField rendering issue

Hi All,

 

i am trying to read text from a file in SDCard and display the text on LabelField. But While doing so \r\n characters are printed as well, i mean they are not rendering properly. e.g. \n stands for new line but it is getting displayed as \n

 

 

 

The code is given below--

For reading the text -

private String getTextFromFile() {
        String result = null;

        FileConnection fconn;
        try {
            fconn = (FileConnection)Connector.open(IMAGES_DATAPATH, Connector.READ_WRITE);

            if (fconn.exists())
            {
                InputStream input=fconn.openDataInputStream();
                byte[] data = IOUtilities.streamToBytes(input);
                result = new String(data);
            }
        } catch (IOException e) {
            result = "Excep>>>>>>>"+e.getMessage();
        }
        return result;
    }
   

 

For adding a LabelField:

 

String response = getTextFromFile();

LabelField lbl = new LabelField(response);
 add(lbl);

 

Thanks in advance.

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

Re: LabelField rendering issue

as a sidenote: you should not execute blocking code, like a fileconnection, without a separate thread.
see
http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

check if the characters are maybe escaped ('\\n' instead of '\n'), you would have to process that in this case.
----------------------------------------------------------
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
Developer
Posts: 121
Registered: ‎05-31-2012
My Device: 9900
My Carrier: Airtel

Re: LabelField rendering issue

[ Edited ]

Ok simon i will take care of blocking code thing.

 

Below is the text from file --

 

The rupee dived to a record low of 61.21 against the dollar on Monday after better-than-expected US jobs data raised concerns about the US Fed.\r\n\r\nThe rupee opened at 60.95 a dollar compared with Friday\\\\\\'s close.\r\n\r\nThe rupee bounced back.

 

I am not able to attach screenshot here , it is giving IO error on forum.

 

Please suggest if anything wrong in the text.

 

 

 

 

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

Re: LabelField rendering issue

check the content of the string with the debugger.
maybe a richtextfield would be better to display a longer text.
----------------------------------------------------------
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
Developer
Posts: 121
Registered: ‎05-31-2012
My Device: 9900
My Carrier: Airtel

Re: LabelField rendering issue

[ Edited ]

Simon, i debug and check the content of the string, it was same as in text file--

 

The rupee dived to a record low of 61.21 against the dollar on Monday after better-than-expected US jobs data raised concerns about the US Fed.\r\n\r\nThe rupee opened at 60.95 a dollar compared with Friday\\\\\\'s close.\r\n\r\nThe rupee bounced back.

 

Also i used RichTextField but rendering issue remain same. But i know your point of suggesting RichTextField, because it helps navigating the text line by line which is very helpful for small screen size.

 

 Please suggest what to do now?

 

 

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

Re: LabelField rendering issue

good question. experiment with it. i know i have used '\n' at least, so that should work.
----------------------------------------------------------
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
Developer
Posts: 121
Registered: ‎05-31-2012
My Device: 9900
My Carrier: Airtel

Re: LabelField rendering issue

Simon i have also used \n many times and never faced such issue

.

But i initialize the String as --

String str =  "new String \n testing";

 

and it works correctly but when i am reading data from file, rendering issue ouccuring.

Please suggest.

 

Rishikesh Singh
Developer
Posts: 121
Registered: ‎05-31-2012
My Device: 9900
My Carrier: Airtel

Re: LabelField rendering issue

[ Edited ]

Things are getting more interesting,

 

When I used the same text to initialize the String instead of reading it from file--

 

 

String response = "The rupee dived to a record low of 61.21 against the dollar on Monday after better-than-expected US jobs data raised concerns about the US Fed.\r\n\r\nThe rupee opened at 60.95 a dollar compared with Friday\\\\\\'s close.\r\n\r\nThe rupee bounced back.";
       

and use it with labelField , It works fine.

 

Now i am curious why is so. Is there any text format issue while reading it from text file OR how it is diffrent to read it from text file and initialize the String by hardcoding it??

 

Any suggestions are Welcome.

 

 

Rishikesh Singh
Developer
Posts: 121
Registered: ‎05-31-2012
My Device: 9900
My Carrier: Airtel

Re: LabelField rendering issue

I request every one to give it a try.. it will occur surely.

 

Speceally Simon, please try once, Please.

 

Thanks in advance.

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

Re: LabelField rendering issue

sorry, i don't have the time to fire up my old java stuff, i am working on bb10 currently.

use the debugger to inspect the two strings and find the differences.
----------------------------------------------------------
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