Use the lwuit-List Control

Source: Internet
Author: User

List controls are frequently used. Today we will introduce how to use list in lwuit:

Code
Private String globalstrurl =   "" ;

Public   Void Initform (){
Removeall ();
Removeallcommands ();
String [] [] straccessories = Esiondata. getinstance ()
. Getsitelist ( " 05342 " );
This . Addcommand ( New Command ( " Return " , 0 ){
Public   Void Actionreceivmed (actionevent eV ){
Formfactory. getinstance (). getmainform (). Show ();
}
});
If (Straccessories ! =   Null ){
Sitelistinfo [] accessoryinfoarray =   New Sitelistinfo [straccessories. Length];
Image img = Getres (). getimage ( " Ydfj.gif " );
For ( Int I =   0 ; I < Straccessories. length; I ++ ){
Accessoryinfoarray [I] =   New Sitelistinfo (straccessories [I] [ 0 ],
Straccessories [I] [ 1 ], IMG );
}
// The settings enable the layers in the selected status of the list to be adaptive enough.
Setlayout ( New Borderlayout ());
Setscrollable ( False );
// Create a list and input the corresponding data
Addcomponent (borderlayout. Center, createlist (accessoryinfoarray,
List. Vertical, New Contactsrenderer ()));
This . Addcommand ( New Command ( " OK " , 1 ){
Public   Void Actionreceivmed (actionevent eV ){
Try {
Esionmidlet. MIDlet. platformrequest (globalstrurl );
} Catch (Connectionnotfoundexception e ){
E. printstacktrace ();
}
}
});
} Else
Addcomponent ( New Label ( " Network exception. No information site found! " ));
}

// List Initialization
Private List createlist ( Final Sitelistinfo [] sitelists, Int Orientation,
Listcellrenderer Renderer ){
Final List list =   New List (sitelists );
List. getstyle (). setbgtransparency ( 0 );
List. setlistcellrenderer (Renderer );
List. setorientation (orientation );
// List list select focus listener event
List. addselectionlistener ( New Selectionlistener (){
Public   Void Selectionchanged ( Int Oldselected, Int Newselected ){
Globalstrurl = Sitelists [list. getselectedindex ()]. siteurl;
}
});
List. addfocuslistener ( New Focuslistener (){
// List to get focus
Public   Void Focusgained (Component CMP ){
Globalstrurl = Sitelists [list. getselectedindex ()]. siteurl;
}

// List loses focus
Public   Void Focuslost (Component CMP ){
Globalstrurl =   "" ;
}
});
// List event-events related to buttons in the middle of the phone
List. addactionlistener ( New Actionlistener (){
Public   Void Actionreceivmed ( Final Actionevent EVT ){
Try {
Esionmidlet. MIDlet. platformrequest (globalstrurl );
} Catch (Connectionnotfoundexception e ){
E. printstacktrace ();
}
}
});
Return List;
}

// List Data Binding
Class Contactsrenderer Extends Container Implements Listcellrenderer {
Private Label name =   New Label ( "" );
Private Label PIC =   New Label ( "" );
Private Label focus =   New Label ( "" );

Public Contactsrenderer (){
Setlayout ( New Borderlayout ());
// Set the icon of each column in list
Addcomponent (borderlayout. West, PIC );
Container CNT =   New Container ( New Boxlayout (boxlayout. y_axis ));
Name. getstyle (). setbgtransparency ( 0 );
Name. getstyle (). setfont (
Font. createsystemfont (font. face_system, Font. style_bold,
Font. size_medium ));
// Set the content of each column in the list
CNT. addcomponent (name );
Addcomponent (borderlayout. Center, CNT );
// Here, you can set the selected color or not through the lwuit Resource Manager.
// However, it cannot overlap with the background color. Otherwise, the effect will not be visible. In fact, this setting is required only for mobile phones with maxcompute.
Focus. getstyle (). setbgcolor ( 0 xffffff );
Focus. getstyle (). setbgtransparency ( 100 );
}

Public Component getlistcellrenderercomponent (list, object value,
Int Index, Boolean Isselected ){
Sitelistinfo sitelist = (Sitelistinfo) value;
Name. settext (sitelist. getsitename ());
PIC. seticon (sitelist. getpic ());
Return   This ;
}

Public Component getlistfocuscomponent ( Final List list ){
Return Focus;
}
}
// Create a class to store the data required in the list. Similarly, if you want to pass the value to another form or the data displayed in the list column
Class Sitelistinfo {
Private String sitename;
Private String siteurl;
Private Image PIC;

Public Sitelistinfo (string sitename, string siteurl, image pic ){
This . Sitename = Sitename;
This . Siteurl = Siteurl;
This . PIC = PIC;
}

PublicString getsitename (){
ReturnSitename;
}

PublicString getsiteurl (){
ReturnSiteurl;
}

PublicImage getpic (){
ReturnPIC;
}
}

Note that the items and their descriptions are marked on the source code, and the use of list is relatively simple. However, it is really necessary to combine the tab and list, in the future, a tab will be used in combination with the listArticle.

:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.