<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaBeat &#187; BlackBerry</title>
	<atom:link href="http://www.javabeat.net/category/mobile/blackberry/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javabeat.net</link>
	<description>Java Technology News</description>
	<lastBuildDate>Wed, 22 May 2013 01:42:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>BlackBerry Java Application Development</title>
		<link>http://www.javabeat.net/2010/10/blackberry-java-application-development/</link>
		<comments>http://www.javabeat.net/2010/10/blackberry-java-application-development/#comments</comments>
		<pubDate>Fri, 01 Oct 2010 00:41:17 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[BlackBerry]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=1757</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>BlackBerry Java Application Development Beginner&#8217;s Guide The book teaches how to write rich, interactive, and smart BlackBerry applications in Java. It expects the readers to know Java but not Java Mobile or the BlackBerry APIs. This book will cover Uiprogramming, data storage, programming network, and Internet APiapps. As we move on, you will learn more [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><a id="dd_start"></a><p><H1><CENTER>BlackBerry Java Application Development</CENTER></H1><br />
<H1>Beginner&#8217;s Guide</H1><br />
<P>The book teaches how to write rich, interactive, and smart BlackBerry applications in<br />
Java. It expects the readers to know Java but not Java Mobile or the BlackBerry APIs.<br />
This book will cover Uiprogramming, data storage, programming network, and Internet<br />
APiapps. As we move on, you will learn more about the BlackBerry’s device features,<br />
such as messaging, GPS, multimedia, contacts and calendar, and so on. This book also<br />
helps you build your own applications to illustrate the platform and the various<br />
capabilities that developers can use in their programs.</P><br />
<H1>What This Book Covers</H1><br />
<P><I>Chapter 1, Introducing BlackBerry Application Development gets</I> you started by talking<br />
about the capabilities of a BlackBerry smartphone and what kind of things can be done<br />
with these capabilities with a custom application. It talks about the other tools which are<br />
available and why writing native Java applications by using the BlackBerry SDK is the<br />
most powerful and practical approach to developing applications. Finally, it covers how<br />
to select which version of the SDK to use and when you might want to use an older<br />
version of the SDK instead of the latest.</P><br />
<P><I>Chapter 2, Installing the development Environment</I> steps you through the process of<br />
installing the proper versions of Java and Eclipse. This chapter talks about when to install<br />
additional versions of the SDK and how to do so through the Eclipse over-the-air update<br />
tool as well as how to install them manually.</P><br />
<P><I>Chapter 3, Getting Familiar with the Development Environment</I> starts off the learning<br />
process by importing an existing sample application—the standard &#8220;Hello World&#8221;<br />
application. After importing the project, the chapter will go over this simple application<br />
line-by-line. Afterwards, you will run the application in the simulator and then introduce<br />
a bug into the application so that you can debug it as well.</P><br />
<P><I>Chapter 4, Creating your first BlackBerry Project</I> is where you create a new project from<br />
scratch. This chapter demonstrates how you accomplish this using Eclipse and the<br />
various wizards that are available within it. It also demonstrates how you can create a<br />
simple, but complete application quickly using the User Interface (UI) elements provided<br />
by the framework.</P><br />
<P><I>Chapter 5, Learning the Basics about the UI</I> creates an application to demonstrate each<br />
of the Uielements that are available to you when using the BlackBerry SDK. This<br />
sample application demonstrates how to set and retrieve data from each field and<br />
discusses when each field should be used according to the BlackBerry development<br />
guidelines. By demonstrating each field, you will get a complete understanding of the<br />
capabilities of each field.</P><br />
<P><I>Chapter 6, Going Deeper into the UI</I> picks up where the previous chapter leaves off by<br />
demonstrating how to use some of the advanced fields, such as lists and trees. It also<br />
covers navigation between screens, displaying dialogs, and common patterns used in the<br />
BlackBerry SDK. By the time you are done with this chapter, you will be well equipped<br />
to create the entire Uifor an application.</P><br />
<P><I>Chapter 7, Storing Data</I> jumps right into how to use the data storage tools of the SDK<br />
and when it is appropriate to use each one. This covers the Java standard RMS, the<br />
BlackBerry-specific <I>PersistentStore</I>, and even how to access the removable media cards<br />
that are available on some devices.</P><br />
<P><I>Chapter 8, Interfacing with Applications</I> shows you how to take advantage of one of<br />
most powerful features available to a BlackBerry application. Each BlackBerry device<br />
comes with standard applications that you can interface with. These include the address<br />
book, calendar, and even the messaging applications. Being able to tightly integrate an<br />
application with these can make it even more valuable and useful to the end user.</P><br />
<P><I>Chapter 9, Networking</I> wades into the complex, but an important area of how to make an<br />
application networking aware. Here, you will discover what transports are available, how<br />
to open connections , and how to send data through them. The sample also demonstrates<br />
how to communicate with a simple web service and parse the resulting XML data.</P><br />
<P><I>Chapter 10, Advanced Topics</I> covers two distinct, but powerful topics. The first topic is<br />
how to utilize the built-in GPS receiver that is built in to some smartphones in order to<br />
get location information. You will learn about the various methods that can be used to get<br />
location information and how to do some common calculations using these coordinates.<br />
The other topic covered in this chapter covers how to use alternate entry points so that a<br />
single project can be used to launch multiple applications. Because these applications<br />
share a common project, they can share code and even memory.</P><br />
<P><I>Chapter 11, Wrapping It All Up</I> finishes the book with tasks that commonly are done last,<br />
such as localization with language resource files and code-signing your application so<br />
that it can be installed on real devices. You will also learn what it takes to distribute your<br />
new application through the BlackBerry App World marketplace.</P><br />
<H1><CENTER>Interfacing with Applications</CENTER></H1><br />
<P><I>Now that we&#8217;ve covered some of the basics of application development it&#8217;s<br />
time to expand our horizons a bit and look at interfacing with other BlackBerry<br />
applications. You can create many great applications that don&#8217;t do anything<br />
more complex than interfacing with the user and storing data. Sometimes<br />
though, developing an interesting application means doing something more<br />
or something special with the existing applications on the device. When you<br />
think about development from this point of view, there are suddenly a lot more<br />
applications that can be created than before.</P><br />
<P>The BlackBerry handhelds come pre-loaded with many great programs to help<br />
make a person more productive. While messages may be the most common<br />
reason a person will purchase a BlackBerry, the other </I><B>Personal Information<br />
Management (PIM)</B><I> applications oft en quickly become essential as well.</P><br />
<P>As a developer, you cannot ignore the other applications on the handheld. The<br />
more integrated an application can be with these standard applications, the<br />
bett er the user experience will generally be. Our TipCalc application is very<br />
specialized, and one of the few that works well without integrating with other<br />
applications. More oft en than not though, any applications that you create will<br />
benefit from some level of integration.</P><br />
<P>Not only can you interface with these applications by adding or editing content<br />
in them, you can also listen for events that allow you to react to things that<br />
happen. Some of the applications even allow you to add menu items and other<br />
&#8220;active content&#8221; to them. That&#8217;s a lot to talk about, so we&#8217;ll just focus on some<br />
of the most common tasks to get you started in this chapter.</I></P><br />
<H1>Introducing PIM</H1><br />
<P>The first area that we will take a look at is the Personal Information Management, or PIM<br />
applications and data. <B>PIM applications</B> are a rather generic name for a group of tools that<br />
manage your personal information, especially as it relates to your handheld. This could be<br />
stretched to include a lot of things, but it generally means your messages, contacts, and<br />
scheduling information that help you to manage your life. In BlackBerry terms it means the<br />
<B>Messages, Address Book, Calendar, Tasks,</B> and <B>Notes</B>.</P><br />
<P>Access to the PIM data in the BlackBerry SDK is provided through the <B>JSR-75 specification</B>,<br />
which is a Java standard. Like many of the Java standards in the BlackBerry SDK, there<br />
are also BlackBerry-specific extensions available that expand the basic classes with new<br />
BlackBerry-specific functionality.</P><br />
<P>Like many of the other standards we find in Java, JSR-75 implements a factory patt ern where<br />
one class, in this case the PIM class, is used to create objects for the other more specific<br />
types of PIM data. The PIM class can basically do only one thing and that is to retrieve a<br />
PIMList object that contains a bunch of specialized PIMItem objects.</P><br />
<H1>Why is all so generic?</H1><br />
<P>All of these PIM classes may seem very generic and you would be absolutely correct. They<br />
are generic and they are supposed to be that way. PIM data is a very generic concept so the<br />
implementation is very generic as well. Also, because it is a Java standard, it needs to be<br />
fl exible enough to accommodate any system that it might be implemented on.</P><br />
<P>A perfect example of this kind of fl exibility is the <B>BlackBerry PIN</B> field. The <B>BlackBerry PIN</B> is<br />
an entry in your address book and therefore, it should be included in the PIM data that you<br />
get. However, a <B>PIN</B> is a BlackBerry-specific concept and no other device out there will use it.<br />
You can&#8217;t really expect the Java standard to include specialized fields for every possible piece<br />
of data that some device may have or want to include. The answer to this is to present PIM<br />
data in a key-value pairing so that it is fl exible enough to handle every possible scenario.</P><br />
<P>A <B>key-value</B> pairing is a somewhat technical term to describe the patt ern for storing values<br />
based on a static key. Or, more simply, if you know the proper key you can access the value.<br />
The fl exible part is that the PIM object storing all of the values does not need to know about<br />
each specific value or provide any special mechanism for accessing each specific value. All<br />
access is done through generic methods, which also require the key.</P><br />
<P>The difficulty in using this kind of approach is that the keys must be common knowledge. In<br />
addition, simple numeric keys do not support self-documenting code or even easily readable<br />
code. Keys that are string values off er a lot of advantages—in that the keys are much more<br />
readable, but the possibility for mistakes is very great because you don&#8217;t have the compiler<br />
to help ensure that only correct keys are used.</P><br />
<P>To help solve these issues there are derived classes for each type of PIM item. While you can<br />
do nearly everything by using the generic classes that a derived class would off er, iwouldn&#8217;t<br />
recommend it. These classes are here to make your code easier to write and read, and<br />
should be used.</P><br />
<H1>PIMLists</H1><br />
<P>As we said early on, the PIM class is used primarily to retrieve a PIMList object for a<br />
specific kind of PIM item, that is, address book contact, calendar event, and so on. For<br />
each of these types, there is also a specialized class that you can use instead of the generic<br />
PIMList class. Classes such as ContactList, EventList, and ToDoList off er a<br />
specialized version of the more generic PIMList class. These specialized classes are also<br />
part of the Java standard and should be preferred because they off er a few more methods<br />
which are specific to that kind of data.</P><br />
<P>There are BlackBerry-specific versions of these classes as well. Therefore, the<br />
BlackBerryContactList class is the BlackBerry-specific version of the ContactList,<br />
which is in turn a specialized version of PIMList for contact data. Generally speaking, you<br />
will want to use the BlackBerry-specific versions of PIMList classes when making<br />
your applications.</P><br />
<H1>PIMItems</H1><br />
<P>A PIMItem is the generic representation for any piece of PIM data. Just like there are specific<br />
versions of the PIMList, there are also specific versions of the PIMItem class for each<br />
kind of PIMItem. Contact, Event, and ToDo are all specific versions of a PIMItem for<br />
that kind of PIM data. As you might expect, there are BlackBerry-specific versions as well.<br />
BlackBerryContact, BlackBerryEvent, and BlackBerryToDo all exist to extend and<br />
further specialize the standard Java classes.</P><br />
<P>These specialized classes aren&#8217;t as specialized or easy to use as one might expect though.<br />
Providing a method called getFirstName might be really useful, but unfortunately,<br />
you will find nothing of the sort. The specialized classes off er few methods for accessing<br />
data. Instead, they provide static values for the keys used to set and retrieve data from<br />
the PIMItem class . Remember, earlier we noted that one drawback to using this kind of<br />
key-value pairing was that keys were sometimes not clear and that you could not expect<br />
help from the compiler. By providing each key value in the specialized class, both of these<br />
goals are accomplished. The name of the key value now provides a readable name and the<br />
compiler will fl ag an error if there is a typo or problem with the constant value being used.</P><br />
<P>Another aspect of PIMItem is that each value that an item has a specific type associated<br />
with it as well. Some of these are obvious, such as the start date of an event using a Date<br />
type. Some of them, such as the <B>Name</B> field of a <B>Contact</B> that requires an array, are not.<br />
Some fields can be given a subtype as well, such as the <B>Phone</B> field. With the subtype you<br />
can specify what kind of phone number it is: home, work, or mobile. Furthermore, some of<br />
the fields can have multiple occurrences while others cannot. A good example of this is the<br />
<B>Email</B> field in a <B>Contact</B>. A contact is allowed to have up to three e-mail addresses, but there<br />
is no subtype associated with them like there is with phone numbers. The bott om line to<br />
all this is that the PIM items have a defined structure to them and they won&#8217;t allow just any<br />
value to be put into a field. The documentation plays a big role here in understanding this<br />
because there are no field-specific methods to provide some additional assistance in the<br />
proper way to access each field.</P><br />
<H1>Laying the ground work</H1><br />
<P>Still, this is all rather abstract because you haven&#8217;t seen any real code samples yet, so let&#8217;s<br />
get into some code! For this chapter, you will build an application that someone will use to<br />
request some time off from their manager. This is definitely one of those applications that<br />
just can&#8217;t be done without interfacing with other applications on the handheld! To make<br />
getting started a litt le easier we will take the starter TimeOff application from the code<br />
bundle and add to it throughout this chapter.</P><br />
<P>The first task to undertake is one to help make testing and debugging easier. Remember,<br />
you will be working on the simulator, which is essentially a brand new device and which can<br />
be oft en reset. That means you don&#8217;t have any of your contacts there! You will need some<br />
contacts later, so to get started let&#8217;s add a menu item to the application that will create a few<br />
contacts that you can later use to test with.</P><br />
<H2>Time for action – creating test contacts</H2><br />
<OL><br />
<LI>Modify the _AddTestAddressesAction menu item in the<br />
TimeOff project to look like the following completed code.</LI><br />
<P><PRE><CODE><br />
	protected MenuItem _AddTestAddressesAction = new MenuItem(<br />
		&#8220;Add Test Data&#8221;, 800000, 50)<br />
	{<br />
		public void run()<br />
		{<br />
			PIM pimInstance = PIM.getInstance();<br />
			try<br />
			{<br />
				// TODO: Create test contacts<br />
				BlackBerryContactList contacts =<br />
					(BlackBerryContactList)pimInstance.openPIMList(<br />
					PIM.CONTACT_LIST, PIM.READ_WRITE);<br />
				BlackBerryContact newContact1 =<br />
					(BlackBerryContact)contacts.createContact();<br />
				BlackBerryContact newContact2 =<br />
					(BlackBerryContact)contacts.createContact();<br />
				String[] names = new<br />
					String[contacts.stringArraySize(BlackBerryContact.NAME)];<br />
				names[BlackBerryContact.NAME_FAMILY] = &#8220;Smith&#8221;;<br />
				names[BlackBerryContact.NAME_GIVEN] = &#8220;John&#8221;;<br />
				if (contacts.isSupportedArrayElement(Contact.NAME,<br />
					Contact.NAME_SUFFIX))<br />
				{<br />
					names[BlackBerryContact.NAME_SUFFIX]=&#8221;Jr&#8221;;<br />
				}<br />
				newContact1.addStringArray(<br />
					BlackBerryContact.NAME,<br />
					BlackBerryContact.ATTR_NONE,<br />
					names);<br />
				names[Contact.NAME_FAMILY] = &#8220;Doe&#8221;;<br />
				names[Contact.NAME_GIVEN] = &#8220;John&#8221;;<br />
				if (contacts.isSupportedArrayElement(Contact.NAME,<br />
					Contact.NAME_PREFIX))<br />
				{<br />
					names[Contact.NAME_PREFIX] = &#8220;Dr.&#8221;;<br />
				}<br />
				newContact2.addStringArray(Contact.NAME, Contact.ATTR_NONE,<br />
					names);<br />
				//TODO: Add Phone numbers<br />
				//TODO: Add Email Addresses<br />
				//TODO: Add Addresses<br />
				newContact1.commit();<br />
				newContact2.commit();<br />
			}<br />
			catch (PIMException e)<br />
			{<br />
				// TODO Auto-generated catch block<br />
				e.printStackTrace();<br />
			}<br />
		}<br />
	};<br />
</CODE></PRE></P><br />
<LI>Then add this line to the constructor to make the menu<br />
item available when you run the application.</LI><br />
<P><PRE><CODE><br />
	this.addMenuItem(_AddTestAddressesAction);<br />
</CODE></PRE></P><br />
</OL><br />
<H3>What just happened?</H3><br />
<P>This is the first of several baby steps as you work towards the goal of creating some test<br />
contacts in the address book in the simulator. As the address book in the simulator doesn&#8217;t<br />
have any entries to begin with, and can be erased frequently, doing this provides you with<br />
a way to quickly and easily create or recreate the contacts you will use later on for testing<br />
other parts of this application. It also happens to be a great way to demonstrate how to add<br />
contacts.</P><br />
<P>This first baby step does only a few things. First, it gets a PIMList object for contacts and<br />
then creates two new contacts. Aft er this it sets the name of each one and finally commits<br />
the records into the address book. These steps make sense at a high level, but let&#8217;s take a<br />
look at the details.</P><br />
<P>The first step is to get an instance to the PIM object, which is done through a static method<br />
in the PIM class called getInstance.</P><br />
<P><PRE><CODE><br />
	PIM pimInstance = PIM.getInstance();<br />
</CODE></PRE></P><br />
<P>Once you have an instance of PIM, the next step is to get a list of contact items using the<br />
openPIMList method on the instance you just retrieved. This same method is used to<br />
get a list of any kind of PIM item so you must specify the type of data to get as one of the<br />
parameters. The PIM class off ers constant values for every kind of PIM item, so in this case,<br />
use the constant PIM.CONTACT_LIST. As you plan to add new contacts, the next parameter<br />
needs to be the constant PIM.READ_WRITE so that you have write permissions. It&#8217;s always<br />
good practice to request the minimum amount of permissions that you need, so if your<br />
application doesn&#8217;t change or add data to the list you should simply use the<br />
PIM.READ permission.</P><br />
<P>As we touched on earlier, this method returns a generic PIMList type so you also<br />
have to cast it to the appropriate specialized type. If a list type of CONTACT_LIST has<br />
been specified, you can cast the resulting PIMList to either of the available specialized<br />
classes—ContactList or BlackBerryContactList. As long as your application is a<br />
BlackBerry-specific application, there is no good reason to use the less specialized class of<br />
ContactList. Instead, you should always use BlackBerryContactList.</P><br />
<P><PRE><CODE><br />
	BlackBerryContactList contacts =<br />
		(BlackBerryContactList)pimInstance.openPIMList(PIM.CONTACT_LIST, PIM.<br />
		READ_WRITE);<br />
</CODE></PRE></P><br />
<P>The next step is to create a couple of new contacts that you will start to populate. This<br />
is done through the createContact method available on the ContactList class.<br />
Again, you need to cast the resulting objects to the proper type. The createContact<br />
method returns a contact, but again you&#8217;ve chosen to use the more specialized version of<br />
BlackBerryContact instead. Because this is all being executed on a BlackBerry handheld,<br />
you can always cast a contact to a BlackBerryContact safely. The same is true for each of<br />
the Java specialized classes and their corresponding BlackBerry specialized class.</P><br />
<P><PRE><CODE><br />
	BlackBerryContact newContact1 = (BlackBerryContact) contacts.<br />
									createContact();<br />
	BlackBerryContact newContact1 = (BlackBerryContact) contacts.<br />
									createContact();<br />
</CODE></PRE></P><br />
<P>The next segment of code sets the name att ribute of the newly created contacts. Notice that<br />
this is done through an array of String objects instead of individual methods. This isn&#8217;t<br />
something that is done to be more efficient, it is done this way because it must be; there is<br />
no other way.</P><br />
<P>We mentioned before about each field in a PIMItem having a type associated with it. Most<br />
of the field types are basic String or Date type fields, but NAME is more complicated than<br />
most of the other fields. The NAME field is defined as a StringArray because there are<br />
many parts to a name and you want to be able to set each part separately. There aren&#8217;t very<br />
many fields of this type used, but this is probably one of the most important. You can only<br />
set the NAME field as a whole unit, so if only one part of the name needs to be changed the<br />
entire name field must be replaced.</P><br />
<P>To work with the name, you must first create a string array of the proper size. There is no<br />
constant value for this as it may vary with the SDK version. Instead, you must first get the size<br />
by using the stringArraySize method on the ContactList and then construct a new<br />
array by using the returned value.</P><br />
<P><PRE><CODE><br />
	String[] names = new String[contacts.stringArraySize(BlackBerryConta<br />
		ct.NAME)];<br />
</CODE></PRE></P><br />
<P>Once you have an array of the proper size each part of the name is set by indexing the array<br />
by using the NAME constant from the Contact class.</P><br />
<P><PRE><CODE><br />
	names[BlackBerryContact.NAME_FAMILY] = &#8220;Smith&#8221;;<br />
	names[BlackBerryContact.NAME_GIVEN] = &#8220;John&#8221;;<br />
</CODE></PRE></P><br />
<P>In this example, you also want to add another name part but are not sure whether the field<br />
is supported in this system. Not all fields are supported and not all of the name subfields are<br />
supported either. You can test to see whether a field or a subfield is supported by using the<br />
isSupportedField or isSupportedArrayElement method s in the ContactList class.<br />
In this case, you test to see if the suffix is supported, and then set the suffix if so.</P><br />
<P><PRE><CODE><br />
	if (contacts.isSupportedArrayElement(Contact.NAME,<br />
									Contact.NAME_SUFFIX))<br />
	{<br />
		names[BlackBerryContact.NAME_SUFFIX]=&#8221;Jr&#8221;;<br />
	}<br />
</CODE></PRE></P><br />
<P>This step is very important if you want to use the same code for multiple platf orms. Each<br />
system can support the fields it chooses. In this case, the suffix is NOT supported and if you<br />
were to step through this code in the debugger, you would see that the code to set the suffix<br />
is skipped over. Later on, when you test this application, you will also see that the suffix was<br />
not added to the contact.</P><br />
<P>Other platf orms may implement it diff erently. You could just assume each of the name<br />
subfields are supported and set the field without testing to see if it is supported. In the<br />
BlackBerry SDK, unsupported fields are just quietly ignored. This can lead to confusion<br />
wondering why a field doesn&#8217;t appear in the <B>Address Book</B> application, but it won&#8217;t cause an<br />
error.</P><br />
<P>The next step is to actually add the NAME field to the contact. Up until this time you&#8217;ve simply<br />
been building an array in memory with all of the proper values.</P><br />
<P><PRE><CODE><br />
	newContact1.addStringArray(<br />
		BlackBerryContact.NAME,<br />
		BlackBerryContact.ATTR_NONE,<br />
		names);<br />
</CODE></PRE></P><br />
<P>Notice that the method addStringArray doesn&#8217;t give any indication about what field is<br />
being added, but only what type of data is being added. All of the PIMItem methods are like<br />
this. Remember, this class is designed to be generic. The first parameter is the field indicator,<br />
which is one of the many constants that are defined in the Contact class. In this case, we<br />
use the BlackBerryContact class . Because BlackBerryContact derived from Contact,<br />
all of the constant values are accessible. The BlackBerryContact class does define<br />
some constants that are BlackBerry-specific, such as PIN. For this field you must reference<br />
the constant value from BlackBerryContact because the Java standard Contact class<br />
does not define it. Partly for this reason, isuggest always referencing constant values from<br />
BlackBerryContact because all of the constant values will be available through this class.</P><br />
<P>The method addStringArray was chosen because that is the type of data that you are<br />
adding. The NAME field is defined as a string array and so you must use the addStringArray<br />
method because it corresponds to the data type of the field.</P><br />
<P>Once you finish with the first contact, the code starts building the NAME string array to add<br />
a second contact. For demonstration sake, all of the constant values that are referenced are<br />
done so using the Contact class instead of the BlackBerryContact class.</P><br />
<P><PRE><CODE><br />
	names[Contact.NAME_FAMILY] = &#8220;Doe&#8221;;<br />
	names[Contact.NAME_GIVEN] = &#8220;John&#8221;;<br />
	if (contacts.isSupportedArrayElement(Contact.NAME,<br />
		Contact.NAME_PREFIX))<br />
	{<br />
		names[Contact.NAME_PREFIX] = &#8220;Dr.&#8221;;<br />
	}<br />
	newContact2.addStringArray(Contact.NAME, Contact.ATTR_NONE, names);<br />
</CODE></PRE></P><br />
<P>Also, notice that the second contact applies a prefix to the name and tests to see if it is<br />
supported in the same way as you did for the suffix when adding the previous contact.<br />
However, the prefix is supported and if you were to step through this method in the<br />
debugger, you would see that the prefix is being set properly.</P><br />
<P>The last step you have to do is to commit the data that has been added to the contact.</P><br />
<P><PRE><CODE><br />
	newContact1.commit();<br />
	newContact2.commit();<br />
</CODE></PRE></P><br />
<P>Simply creating a new contact is not enough; you must commit the changes in it by using the<br />
commit method. Creating a contact and then never committing it will not have any eff ect on<br />
the <B>Address Book</B> application. It simply won&#8217;t be shown in the list. That&#8217;s the whole point of<br />
this exercise, so you have to make sure and commit the changes once they are all done.<br />
At this point, if you were to run the application and select the menu, you would see two new<br />
contacts added to the Address Book application in the simulator. They would show up as <B>Dr.<br />
John Doe</B> and <B>John Smith</B>. There would be only names with these contacts because that is all<br />
that you&#8217;ve added so far.</P><br />
<P>In the example code that you just stepped through there was one mistake that could have<br />
proven to be very serious. Did you catch it? You are reusing the names array to set the<br />
names of both contacts. This is actually risky, but it happens to work out in this case. If the<br />
SUFFIX field had been supported then your <B>Dr. John Doe</B> would have actually been <B>Dr. John<br />
Doe Jr.</B> because the array was not reset before it was used again. If you had changed the<br />
order around, <B>John Smith</B> would have been <B>Dr. John Smith</B>. This might have lead to a bug<br />
that could have been tough to track down, so keep it in mind.</P></p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<div class='dd_outer'><div class='dd_inner'><div id='dd_ajax_float'><div class='dd_button_v'><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Fwww.javabeat.net%2F2010%2F10%2Fblackberry-java-application-development%2F" send="false" show_faces="false"  layout="box_count" width="50"  ></fb:like></div><div style='clear:left'></div><div class='dd_button_v'><script type='text/javascript' src='https://apis.google.com/js/plusone.js'></script><g:plusone size='tall' href='http://www.javabeat.net/2010/10/blackberry-java-application-development/'></g:plusone></div><div style='clear:left'></div><div class='dd_button_v'><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.javabeat.net/2010/10/blackberry-java-application-development/" data-count="vertical" data-text="BlackBerry Java Application Development" data-via="javabeat" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style='clear:left'></div><div class='dd_button_extra_v'><script type="text/javascript">jQuery(document).load(function(){ stLight.options({publisher:'bab47279-62c9-46af-addc-79fd1fe8fee0'}); });</script><div class="st_email_custom"><span id='dd_email_text'>email</span></div></div><div style='clear:left'></div><div class='dd_button_extra_v'><div id='dd_print_button'><span id='dd_print_text'><a href='javascript:window:print()'>print</a></span></div></div><div style='clear:left'></div></div></div></div><script type="text/javascript">var dd_offset_from_content = 44; var dd_top_offset_from_content = 0;</script><script type="text/javascript" src="http://www.javabeat.net/wp-content/plugins/digg-digg//js/diggdigg-floating-bar.js?ver=5.3.0"></script><div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2010/10/blackberry-java-application-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
