×
Namespaces

Variants
Actions
(Difference between revisions)

Using List in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
m (Protected "CS001266 - Using List in Java ME" [edit=sysop:move=sysop])
hamishwillee (Talk | contribs)
m (Hamishwillee - Add link to Projects)
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Java ME]][[Category:UI]][[Category:Series 40]][[Category:Symbian]][[Category:Series 40 3rd Edition FP1]][[Category:S60 3rd Edition (initial release)]][[Category:S60 3rd Edition FP1]][[Category:S60 3rd Edition FP2]][[Category:Series 40 6th Edition FP1]][[Category:Series 40 Developer Platform 2.0]][[Category:Nokia Belle]][[Category:Code Examples]]
__NOEDITSECTION__
+
{{Abstract|The code example demonstrates how to use several standard types of LCDUI listboxes.}}
{{CodeSnippet
+
{{SeeAlso|[https://projects.developer.nokia.com/s40uivisualisation Series 40 UI Component Demos] (Projects)}}
|id= 
+
{{ArticleMetaData <!-- v1.2 -->
|platform=Series 40 3rd Edition, FP1, S60 3rd Edition, S60 3rd Edition, FP1, S60 3rd Edition, FP2
+
|sourcecode= [[Media:Using ListBox in Java ME.zip]]
|devices=Nokia 6131, Nokia N81
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|category=Java ME
+
|devices= Nokia 6131, Nokia N81, Nokia C3-01, Nokia Asha 306, Nokia E7-00
|subcategory=UI
+
|sdk= [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 1.1 for Java], [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 2.0 for Java (beta)], [http://www.developer.nokia.com/info/sw.nokia.com/id/ec866fab-4b76-49f6-b5a5-af0631419e9c/S60_All_in_One_SDKs.html/ Nokia Symbian SDKs]
|creationdate=November 1, 2008
+
|platform= Series 40, S60, Nokia Belle
|keywords=javax.microedition.lcdui.List, javax.microedition.lcdui.Choice,
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
javax.microedition.lcdui.Choice.IMPLICIT, javax.microedition.lcdui.Choice.EXCLUSIVE, javax.microedition.lcdui.Choice.MULTIPLE, javax.microedition.lcdui.List.getSelectedFlags, javax.microedition.lcdui.List.getSelectedIndex, javax.microedition.lcdui.List.setSelectCommand, ListBoxDemo.createListBox, ListBoxDemo.getSelectedItems
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= javax.microedition.lcdui.List, javax.microedition.lcdui.Choice,
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20081127
 +
|author= [[User:Olkazmin]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001266
 
}}
 
}}
 
  
 
==Overview==
 
==Overview==
  
The code snippet demonstrates how to use several standard types of listboxes.
+
There are four types of lists in MIDP2.0 / LCDUI:
 
+
There are four types of lists in MIDP2.0:
+
 
+
 
<code java>
 
<code java>
 
Choice.IMPLICIT
 
Choice.IMPLICIT
Line 26: Line 37:
 
</code>
 
</code>
  
<tt>List</tt> implements the <tt>Choice</tt> interface. Only first three can be used for List objects.  
+
{{Icode|List}} implements the {{Icode|Choice}} interface. Only first three can be used for List objects.  
  
<tt>IMPLICIT</tt> - A list of strings. Each item gets selected when a cursor moves over it. <tt>List.setSelectCommand</tt> can be used with this type of List. You can redefine a Command that will be triggered on selection. Selected item index can be retrieved using the <tt>List.getSelectedIndex</tt> method.
+
{{Icode|IMPLICIT}} - A list of strings. Each item gets selected when a cursor moves over it. {{Icode|List.setSelectCommand}} can be used with this type of List. You can redefine a Command that will be triggered on selection. Selected item index can be retrieved using the {{Icode|List.getSelectedIndex}} method.
  
<tt>EXCLUSIVE</tt> A choice that has exactly one element selected at time. An item gets selected only when you move the cursor over it and press 'Select'.  The selected item index can be retrieved using the <tt>List.getSelectedIndex</tt> method.
+
{{Icode|EXCLUSIVE}} A choice that has exactly one element selected at time. An item gets selected only when you move the cursor over it and press 'Select'.  The selected item index can be retrieved using the {{Icode|List.getSelectedIndex}} method.
  
<tt>MULTIPLE</tt> - A list of checkboxes. This type of list provides multiple selection functionality. Selected items can be retrieved using the <tt>List.getSelectedFlag</tt> method.
+
{{Icode|MULTIPLE}} - A list of checkboxes. This type of list provides multiple selection functionality. Selected items can be retrieved using the {{Icode|List.getSelectedFlag}} method.
  
 
==Source file: ListBoxDemo.java==
 
==Source file: ListBoxDemo.java==
Line 220: Line 231:
 
==Postconditions==
 
==Postconditions==
  
When the application starts, an <tt>IMPLICIT</tt> listbox is being opened.  
+
When the application starts, an {{Icode|IMPLICIT}} listbox is being opened.  
  
 
'Show selected' command displays currently selected item/items in the title bar.
 
'Show selected' command displays currently selected item/items in the title bar.
  
For <tt>IMPLICIT</tt> listbox, when you press 'Select', the title is immidiately changed.  
+
For {{Icode|IMPLICIT}} listbox, when you press 'Select', the title is immidiately changed.  
  
For <tt>EXCLUSIVE</tt> listbox items, selection status changes only when you press 'Select'.  
+
For {{Icode|EXCLUSIVE}} listbox items, selection status changes only when you press 'Select'.  
  
<tt>MULTIPLE</tt> listbox have check/uncheck commands instead of 'Select'.  
+
{{Icode|MULTIPLE}} listbox have check/uncheck commands instead of 'Select'.  
  
After checking the specific items, their indexes can be retrieved by using <tt>List.getSelectedFlags</tt>.
+
After checking the specific items, their indexes can be retrieved by using {{Icode|List.getSelectedFlags}}.
  
 
==Supplementary material==
 
==Supplementary material==
  
You can view the source file and executable application in the attached zip archive.  The archive is available for download at [[ Media:Using_ListBox_in_Java_ME.zip]]
+
You can view the source file and executable application in the attached zip archive.  The archive is available for download at [[ Media:Using ListBox in Java ME.zip]].
 
+
[[Category:Java ME]][[Category:Code Examples]][[Category:UI]]
+

Revision as of 09:48, 5 October 2012

The code example demonstrates how to use several standard types of LCDUI listboxes.

See Also
Article Metadata
Code ExampleTested with
Devices(s): Nokia 6131, Nokia N81, Nokia C3-01, Nokia Asha 306, Nokia E7-00
CompatibilityArticle
Keywords: javax.microedition.lcdui.List, javax.microedition.lcdui.Choice,
Created: olkazmin (27 Nov 2008)
Last edited: hamishwillee (05 Oct 2012)

Contents

Overview

There are four types of lists in MIDP2.0 / LCDUI:

Choice.IMPLICIT
Choice.EXCLUSIVE
Choice.MULTIPLE
Choice.POPUP

List implements the Choice interface. Only first three can be used for List objects.

IMPLICIT - A list of strings. Each item gets selected when a cursor moves over it. List.setSelectCommand can be used with this type of List. You can redefine a Command that will be triggered on selection. Selected item index can be retrieved using the List.getSelectedIndex method.

EXCLUSIVE A choice that has exactly one element selected at time. An item gets selected only when you move the cursor over it and press 'Select'. The selected item index can be retrieved using the List.getSelectedIndex method.

MULTIPLE - A list of checkboxes. This type of list provides multiple selection functionality. Selected items can be retrieved using the List.getSelectedFlag method.

Source file: ListBoxDemo.java

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
 
 
public class ListBoxDemo extends MIDlet implements CommandListener
{
private Display display;
/**
* IMPLICIT listbox.
*/

private List lbxImpl;
/**
* EXPLICIT listbox.
*/

private List lbxExcl;
/**
* MULTIPLE listbox.
*/

private List lbxMult;
/**
* Item count.
*/

private int arrItemCount;
/**
* Selection command for IMPLICIT listbox.
*/

private Command cmdSel;
/**
* Command that gets currently selected items.
*/

private Command cmdSelected;
/**
* Calls exitMIDlet method.
*/

private Command cmdExit;
/**
* Shows IMPLICIT listbox.
*/

private Command cmdImpl;
/**
* Shows EXPLICIT listbox.
*/

private Command cmdExcl;
/**
* Shows MULTIPLE listbox.
*/

private Command cmdMult;
/**
* Constructor. Constructs the object and initializes displayables.
*/

public ListBoxDemo() {
display = Display.getDisplay( this );
arrItemCount = 5;
String[] arrItems = new String[arrItemCount];
for(int i = 0; i < arrItemCount; i++) {
arrItems[i] = "Item " + i;
}
 
cmdExit = new Command( "Exit", Command.EXIT, 1 );
cmdImpl = new Command( "Implicit", Command.SCREEN, 1);
cmdExcl = new Command( "Exclusive", Command.SCREEN, 1);
cmdMult = new Command( "Multiple", Command.SCREEN, 1);
cmdSelected = new Command( "Show selected", Command.SCREEN, 1 );
cmdSel = new Command( "Select", Command.ITEM, 1 );
lbxImpl = createListBox( List.IMPLICIT, arrItems );
lbxExcl = createListBox( List.EXCLUSIVE, arrItems );
lbxMult = createListBox( List.MULTIPLE, arrItems );
}
/**
* Creates a List object instance, fills it with data
* and adds commands to it.
* @param lbxType - holds type of listbox to create.
* @param arrItems - array of string items to fill listbox with.
* @return Reference to created List object instance.
*/

private List createListBox( int lbxType, String[] arrItems ) {
List lbxTemp = null;
lbxTemp = new List( "Listbox",
lbxType, //setting list box type
arrItems, //setting listbox content
null); //we don't set any images
//setting commands
lbxTemp.addCommand( cmdExit );
lbxTemp.addCommand( cmdImpl );
lbxTemp.addCommand( cmdExcl );
lbxTemp.addCommand( cmdMult );
lbxTemp.addCommand( cmdSelected );
if( lbxType == List.IMPLICIT ) {
lbxTemp.setSelectCommand( cmdSel );
}
lbxTemp.setCommandListener( this );
return lbxTemp;
}
/**
* Returns a String hodling " " separater selected items
* indexes.
*/

private String getSelectedItems( List lbx ) {
boolean[] arrSel = new boolean[lbx.size()];
int selected = lbx.getSelectedFlags( arrSel );
String result = "";
for(int i = 0; i< arrSel.length;i++ ) {
if( arrSel[i] ) {
result = result + " " + i;
}
}
return result;
}
/**
* From MIDlet.
* Called when MIDlet is started.
* @throws javax.microedition.midlet.MIDletStateChangeException
*/

public void startApp() throws MIDletStateChangeException {
display.setCurrent( lbxImpl );
}
/**
* From MIDlet.
* Called to signal the MIDlet to enter the Paused state.
*/

public void pauseApp() {
//No implementation required
}
/**
* From MIDlet.
* Called to signal the MIDlet to terminate.
* @param unconditional whether the MIDlet has to be unconditionally
* terminated
* @throws javax.microedition.midlet.MIDletStateChangeException
*/

public void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param command the command that was invoked
* @param displayable the displayable where the command was invoked
*/

public void commandAction( Command command, Displayable displayable ) {
 
if( command == cmdExit ) {
notifyDestroyed();
}
if( command == cmdSel ) {
lbxImpl.setTitle("Selected: " + lbxImpl.getSelectedIndex() );
}
 
if( command == cmdSelected ) {
if( displayable == lbxImpl ){
lbxImpl.setTitle("Selected: " +
lbxImpl.getSelectedIndex() );
}
if( displayable == lbxExcl ){
lbxExcl.setTitle("Selected: " +
lbxExcl.getSelectedIndex() );
}
if( displayable == lbxMult ){
lbxMult.setTitle("Selected:" + getSelectedItems(lbxMult) );
}
}
if( command == cmdImpl ) {
display.setCurrent( lbxImpl );
}
if( command == cmdExcl ) {
display.setCurrent( lbxExcl );
}
if( command == cmdMult ) {
display.setCurrent( lbxMult );
}
}
}

Postconditions

When the application starts, an IMPLICIT listbox is being opened.

'Show selected' command displays currently selected item/items in the title bar.

For IMPLICIT listbox, when you press 'Select', the title is immidiately changed.

For EXCLUSIVE listbox items, selection status changes only when you press 'Select'.

MULTIPLE listbox have check/uncheck commands instead of 'Select'.

After checking the specific items, their indexes can be retrieved by using List.getSelectedFlags.

Supplementary material

You can view the source file and executable application in the attached zip archive. The archive is available for download at Media:Using ListBox in Java ME.zip.

165 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×