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
mnpaslay
Posts: 105
Registered: ‎03-09-2009
My Device: Not Specified
Accepted Solution

Easy random questions

[ Edited ]

1.  I was trying to simplify an application, changing several String types to integer and now they do not show up on screen using Add, I searched around and my conclusion is that if I want to show an int type I have to convert it to use add, is that correct?

 

2.  I was playing around with private and public in simple hello world application, I expected that if I made a variable public it would be everywhere, but it isn't...example below

 

public class HelloWorld extends UiApplication
{
    public String stringToShow;
   
        public static void main(String[] args)
        {
                //create a new instance of the application
                //and start the application on the event thread
                HelloWorld theApp = new HelloWorld();
                theApp.enterEventDispatcher();
    }
        public HelloWorld()
        {
                //display a new screen
                stringToShow = "Hello World Passed String";
                pushScreen(new HelloWorldScreen());
        }
}

//create a new screen that extends MainScreen, which provides
//default standard behavior for BlackBerry applications
final class HelloWorldScreen extends MainScreen
{
        public HelloWorldScreen()
        {

              //invoke the MainScreen constructor
                super();

            //add a title to the screen
                LabelField title = new LabelField("HelloWorld Sample", LabelField.ELLIPSIS
                                | LabelField.USE_ALL_WIDTH);
                setTitle(title);

//add the text "Hello World!" to the screen
                add(new RichTextField(stringToShow));
        }

}

Message Edited by mnpaslay on 03-20-2009 10:40 AM
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Easy random questions

1. It depends on what you're trying to add to what, but generally you can only add Field objects to Screens. Each Field class, in turn, defines the kinds of data it can work with. A RichTextField, for instance, only knows how to deal with String objects. Check the documentation for each type of UI object you want to use.

 

2. This question is really about Java, not BlackBerry programming. Public members are not "everywhere", but they are "accessible everywhere". The example you posted doesn't work because stringToShow is an instance variable of the HelloWorld class, but you don't have an instance of HelloWorld to work with when you try to use stringToShow in HelloWorldScreen. There are many ways to change your code to something that works. Here's a sampling.

 

  • First, you could modify the HelloWorldScreen constructor to take a String parameter to be the string to show (in which case you don't need to make it public in HelloWorld, but you need to change the call to new HelloWorldScreen().
  • Second, you could make stringToShow a static variable of the class HelloWorld. Then you can get to it using the syntax HelloWorld.stringToShow without having an instance of HelloWorld (basically, it's a global variable).
  • Third, inside the HelloWorldScreen constructor you could grab an instance of HelloWorld to get at the instance variable:

 

HelloWorld app = (HelloWorld) UiApplication.getApplication();

// ...

add(new RichTextField(app.stringToShow));

There are lots of other ways, but this should give you an idea of some possibilities.

 

Hope it helps.

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
mnpaslay
Posts: 105
Registered: ‎03-09-2009
My Device: Not Specified

Re: Easy random questions

[ Edited ]

Ted,

 

thanks for your help, I had figured solutions to both of these problems, but, there are many many ways to solve these.  For question 1, my basic question was I have a variable that will only be an integer, therefore, I feel that I should declare it as an int

 

int myInteger

 

but, to get it to show on the screen on the Blackberry, the way I found to show it was:

 

add (new RichTextField(Integer.toString(myInteger)));  even though it is an integer, perhaps it would be better to just declare it as a string:

 

String myInteger

add (newRichTextField(myInteger));

 

I searched through the different field types and could not find a basic int type for add...I found a lot of ways to add a string though...but no way to show an integer on the screen, which seems like i'm missing something...

 

For the second part, it was more of an understanding what it means to be public versus private...

 

but, when the program runs, it starts with main right?  doesn't that create an instance of HelloWorld than in turn starts an instant of HelloWorldScreen?  does each close when the other opens?

 

HelloWorld app = (HelloWorld) UiApplication.getApplication(); and this line I see in a lot of places of sample code, i believe it retrieves an instance that already exists...so if it already exists why is the variable missing...

 

and, the other side of hte = sign, = (HelloWorld) UiApplication.getApplication(); is that all related to the UiApplicaiton Class? I'm unfamilare with the () after the equal statement...

 

like a integer looks like

int myNumber = 5;

 

I have not see cases where

int myNumber = (int) something.something...

 

 

 

 

 

 

 

Message Edited by mnpaslay on 03-20-2009 04:38 PM
New Developer
declanshanaghy
Posts: 8
Registered: ‎03-20-2009
My Device: Not Specified

Re: Easy random questions

You need to preserve the integrity of the data in your application.

If you're working with numbers you need an int, end of story.

 

An application should be structured in layers. Usuall something like this.

 

 

- UI Layer

- Business logic layer

- Persistence layer

 

 

As you traverse down the layers the data structures usually become less abstract.

In your simple example you have 2 layers. Logic and UI.

 

The data is stored as an int in the logic but needs to be displayed as a string in the UI.

This is a perfectly acceptable practice.

 

 

Developer
bneeman
Posts: 132
Registered: ‎02-11-2009
My Device: Not Specified

Re: Easy random questions

Vector vector; //...

String str = vector.get(1); // doesn't compile - type mismatch

String str = (String) vector.get(1); // does compile

 

Vector.get(int index) returns an Object type. The code assigns the returned value to a String type. The compiler cannot accept that. On the next line type-cast (String) tells the compiler that the programmer knows there's a type mismatch and trusts the data to be correct (if it's not, a ClassCastException would be thrown in runtime).

 

The concept is not unique to JAVA. Neither is the syntax. If you're not familiar with it, brush up on Object Oriented Programming.

 

Cheers, Barak.

Developer
mnpaslay
Posts: 105
Registered: ‎03-09-2009
My Device: Not Specified

Re: Easy random questions

[ Edited ]

I think I understand the concept ok, I just need to learn all the rules and syntax.  I have used other programming languages before.  I have worked through several java tutorials and searched through a lot of sun's site and did not see much if any information on type-casting like below

 

String str = vector.get(1); // doesn't compile - type mismatch

String str = (String) vector.get(1); // does compile

 

anyone have a good link?  My hope is that is also the more advanced syntax i have not seen in the tutorials I have found (anyone have better links for that)

 

On my other question, anyone know a better way to push a int type to the screen other than:

 

add (new RichTextField(Integer.toString(myInteger)));

Message Edited by mnpaslay on 03-20-2009 09:17 PM
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Easy random questions

Some comments:

 

"I have used other programming languages before" - I'm guessing that none of them have been object oriented languages.  This is the big difference between Java and procedural languages.  Until you get your head around Objects, it will all seem very strange.

 

"anyone have a link?"

This is called casting - see

http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html

 

"anyone know a better way"

Not sure what better means in this context, but I presume you want something that looks simpler, in which case, no.

Developer
mnpaslay
Posts: 105
Registered: ‎03-09-2009
My Device: Not Specified

Re: Easy random questions

Not so much looking for simpler just like to learn all the ways to do something...and I'm surprized that a string is easily represented on the screen but there is no method for an integer...it is more a test of my searching skill as well as reading the varioius api documentation.  I was looking at the .screen and field aspects of the blackberry api...as well as through many of the sample applications...

 

That link is the same I have been working though, just had not gotten to that yet.  Object oriented seems fine, just the syntax and rules...though I have been able to get several things I made working on the simulator...I just want to make sure it is optimized.

 

Thanks for your help!