BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 07-28-2005, 08:33 PM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Jun 2005
Model: 7520
Posts: 40
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default BlackBerry Fairy's Basic List

Please Login to Remove!

deepthi, this one is for you dude. Hey, I got it working!

This one is basic and I wrote it using Sun's J2ME Toolkit. For some reason, I am more comfortable with the IDE.

This simple app will list 3 choices for dinner. When you've made a choice, you click on the ok button and it tells you or confirms what you've chosen by adding a tasty comment.

Look at it for what it is, a newbie's form

Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


//------DEFINE CLASS---------------------
//notice that it implements CommandListener
//you need this so you can catch user commands
public class listChoice
 extends MIDlet implements CommandListener, ItemStateListener
{


//------DELCARE VAR!---------------------
 Display display;
 Command exitCommand;
 Command nextCommand;
 Command backCommand;
 String strTitleOfMidlet;
 String theChoice;




//------In order to get stuff going, you need to create an instance
 public listChoice()
 {

//------get the display for this MIDlet
   display = Display.getDisplay(this);
   exitCommand=new Command("Exit", Command.EXIT, 1);
   nextCommand=new Command("Next", Command.OK, 1);
   backCommand=new Command("Back", Command.CANCEL, 1);
 }


//------startApp() starts when instance of MIDlet is good
//------All the good stuff starts here :)
 protected void startApp()
  throws MIDletStateChangeException
 {



//------CALL THE MAIN MENU---------------------
  mainMenu();


//------NOW SHOW YOUR STUFF---------------------
  //Display.getDisplay(this).setCurrent(myForm);
 }


//------HANDLE THE COMMANDS---------------------
 public void commandAction(Command c, Displayable s) {
  if(c==exitCommand){
  notifyDestroyed();

  }else if(c==nextCommand){

   if(theChoice==null){
   okForm("Pizza");
   }else{
   okForm(theChoice);
    }

  }else if(c==backCommand){
  mainMenu();
  }
 }//commandAction


//------HANDLE THE CHOICE BOXES---------------------
 public void itemStateChanged(Item item) {
  ChoiceGroup myChoices = (ChoiceGroup) item;

  //get the which choice
  int i=myChoices.getSelectedIndex();

  //theChoice was defined earlier
  //this gets the text of choice
  //String theChoice=myChoices.getString(myChoices.getSelectedIndex());
   theChoice=myChoices.getString(myChoices.getSelectedIndex());

  //check the console to see the selection
  System.out.println(theChoice);

  //do stuff here
  //
  //okForm(theChoice);
 }


//------OTHER SCREENS YOU CAN CALL---------------------
//------mainMenu/splash screen--------------------
   public void mainMenu() {
   Form mainMenuForm = new Form("MAIN MENU");
      mainMenuForm.setCommandListener(this);


   String strExplanation="This MIDlet contains EXIT, NEXT, AND BACK commands.";
   strExplanation=strExplanation + "\n\nIt also uses a module approach to navigation.";
   strExplanation=strExplanation + "\n\nThis could be your startup screen.";
   //mainMenuForm.append(strExplanation);


   //add the EXIT command
   mainMenuForm.addCommand(exitCommand);
   mainMenuForm.addCommand(nextCommand);
   //myForm.addCommand(backCommand);




  //add a list
  //ChoiceGroup myChoices = new ChoiceGroup("Select Items",Choice.MULTIPLE);
  ChoiceGroup myChoices = new ChoiceGroup("\nWhat's for dinner?",Choice.EXCLUSIVE);
  myChoices.append("Pizza", null);
  myChoices.append("Chicken", null);
  myChoices.append("Indian", null);
  //stick on the form
  mainMenuForm.append(myChoices);
  //remember to add some ears to this so that the app knows what's going on
  mainMenuForm.setItemStateListener(this);


   mainMenuForm.setCommandListener(this);

   display.setCurrent(mainMenuForm);
   }




//------okForm(String theChoice)--------------------

//get the choice that user has chosen
   public void okForm(String theChoice) {

    System.out.println(theChoice);

   Form okForm = new Form("A New Form");

   //Give a custom message based on choice
  if(theChoice.equals(null)){
  okForm.append("You need to choose a dinner.");

  }else if(theChoice.equals("Pizza")){
   okForm.append("The best of sauce and peperoni?");

  }else if(theChoice.equals("Chicken")){
   okForm.append("Tender and juicy!");

  }else{
   okForm.append("The spice is just right!");
  }



   okForm.append("\n\nGuest chose: '" + theChoice + "'.");
      okForm.addCommand(backCommand);
      okForm.setCommandListener(this);
      display.setCurrent(okForm);
   }

//------okForm2--------------------
  public void okForm2() {
     Form okForm2 = new Form("A New Form2");
     okForm2.addCommand(backCommand);
     okForm2.setCommandListener(this);
     display.setCurrent(okForm2);
   }


public void blankMethod()
 {

 }




//------destroyApp() destroys the Midlet

 protected void destroyApp( boolean unconditional )
  throws MIDletStateChangeException
 {}

//------pauseApp() pauses the Midlet
 protected void pauseApp()
 {}

}
Offline  
Old 07-29-2005, 08:23 AM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Dec 2004
Location: in a house...
Model: lots
Carrier: Rogers
Posts: 1,148
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

While we are at it... here's code for a simple list plus a handler.

Code:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import java.util.*;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.EventInjector;
import net.rim.device.api.ui.UiApplication;

class tScreen extends MainScreen
{
    ListField mainList = new ListField();
    ListCallback mainCallback = new ListCallback();
    public tScreen()
    {
        setTitle("This Application");
        String firstField = new String("Pizza");
        String secondField = new String("Wings");
        mainList.setCallback(mainCallback);
        mainList.insert(0);
        mainCallback.insert(firstField, 0);
        mainList.insert(1);
        mainCallback.insert(secondField, 1);
        this.add(mainList);
    }
    public boolean onClose()
    {
        System.exit(0);
        return true;
    }
    public boolean trackwheelClick( int status, int time ) {
        switch(mainList.getSelectedIndex())
        {
            case 0: 
                //This shows how to push another screen
                pizzaScreen pScreen = new pizzaScreen();
                UiApplication.getUiApplication().pushScreen(pScreen);
                break;
            case 1: 
                //This shows how to throw an alert
                Dialog.alert("Wings!"); 
                break;
        };
        return true;
    }
    private static class ListCallback implements ListFieldCallback
    {
        private Vector listElements = new Vector();
        public void drawListRow(ListField list, Graphics g, int index, int y, int w)
        {
            String text = (String)listElements.elementAt(index);
            g.drawText(text, 0, y, 0, w);
        }
        public Object get(ListField list, int index)
        {
            return listElements.elementAt(index);
        }
        public int indexOfList(ListField list, String p, int s)
        {
            return listElements.indexOf(p, s);
        }
        public int getPreferredWidth(ListField list)
        {
            return Graphics.getScreenWidth();
        }
        public void insert(String toInsert, int index)
        {
            listElements.addElement(toInsert);
        }
        public void erase()
        {
            listElements.removeAllElements();
        }
    }
}
The ListCallBack class is char for char from the RIM documentation. I'm sure it's copyrighted and I broke the law by pasting this in, but it's kind of needed for the example. I sure they will understand.

cd.

Last edited by corey@12mile : 07-29-2005 at 08:26 AM. Reason: my bad... again...
Offline  
Old 07-29-2005, 10:12 AM   #3 (permalink)
Knows Where the Search Button Is
 
Join Date: Jun 2005
Model: 7520
Posts: 40
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

cory12mile, thank you! Very cool.
Offline  
Old 07-29-2005, 11:04 AM   #4 (permalink)
BlackBerry Extraordinaire
 
Join Date: Dec 2004
Location: in a house...
Model: lots
Carrier: Rogers
Posts: 1,148
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

We need a codebase where people can post complete and working examples of various tasks. The RIM samples are OK, but usually they are incomplete or extend other class's outside of the current code, which makes things nasty to try and follow through.

I think there is a serious need for more working code to be posted on the net specific to the RIM API's. I do have a few small projects started that will be open sourced when I am done for people, and maybe if Tom feels like it, I can use BBF as a home for them. I'd use SF, but I'd rather BBF get the exposure.

cd.
Offline  
Old 08-17-2005, 03:37 PM   #5 (permalink)
New Member
 
Join Date: Aug 2005
Location: Buford, GA
Model: 7250
Posts: 7
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by corey@12mile
While we are at it... here's code for a simple list plus a handler.

Code:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import java.util.*;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.EventInjector;
import net.rim.device.api.ui.UiApplication;

class tScreen extends MainScreen
{
    ListField mainList = new ListField();
    ListCallback mainCallback = new ListCallback();
    public tScreen()
    {
        setTitle("This Application");
        String firstField = new String("Pizza");
        String secondField = new String("Wings");
        mainList.setCallback(mainCallback);
        mainList.insert(0);
        mainCallback.insert(firstField, 0);
        mainList.insert(1);
        mainCallback.insert(secondField, 1);
        this.add(mainList);
    }
    public boolean onClose()
    {
        System.exit(0);
        return true;
    }
    public boolean trackwheelClick( int status, int time ) {
        switch(mainList.getSelectedIndex())
        {
            case 0: 
                //This shows how to push another screen
                pizzaScreen pScreen = new pizzaScreen();
                UiApplication.getUiApplication().pushScreen(pScreen);
                break;
            case 1: 
                //This shows how to throw an alert
                Dialog.alert("Wings!"); 
                break;
        };
        return true;
    }
    private static class ListCallback implements ListFieldCallback
    {
        private Vector listElements = new Vector();
        public void drawListRow(ListField list, Graphics g, int index, int y, int w)
        {
            String text = (String)listElements.elementAt(index);
            g.drawText(text, 0, y, 0, w);
        }
        public Object get(ListField list, int index)
        {
            return listElements.elementAt(index);
        }
        public int indexOfList(ListField list, String p, int s)
        {
            return listElements.indexOf(p, s);
        }
        public int getPreferredWidth(ListField list)
        {
            return Graphics.getScreenWidth();
        }
        public void insert(String toInsert, int index)
        {
            listElements.addElement(toInsert);
        }
        public void erase()
        {
            listElements.removeAllElements();
        }
    }
}
The ListCallBack class is char for char from the RIM documentation. I'm sure it's copyrighted and I broke the law by pasting this in, but it's kind of needed for the example. I sure they will understand.

cd.
I like the ListField code example you posted, is there a way to get the selected text out of the list? In other words, when the user clicks on the trackwheel you do a getSeletedIndex, but I would like to get the selected text as well. Thanks in advance.
Offline  
Old 08-18-2005, 08:04 AM   #6 (permalink)
BlackBerry Extraordinaire
 
Join Date: Dec 2004
Location: in a house...
Model: lots
Carrier: Rogers
Posts: 1,148
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just off the top of my head (which means its real early and I haven't had any coffee yet). This should either work, or give you an idea of what you have to do. But like I said, I haven't tested this, so play with it and let us know the results, please.

Code:
String selectedtext = (String)mainList.elementAt( mainList.getSelectedIndex());
cd.

Last edited by Mark Rejhon : 08-18-2005 at 10:32 AM. Reason: Fix linebreak
Offline  
Old 08-18-2005, 11:03 AM   #7 (permalink)
New Member
 
Join Date: Aug 2005
Location: Buford, GA
Model: 7250
Posts: 7
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, I ended up getting the code below to work as well.

String sYear = (String)mainCallback.get(mainList, iYear);
Offline  
Old 08-18-2005, 01:07 PM   #8 (permalink)
New Member
 
Join Date: Aug 2005
Location: Buford, GA
Model: 7250
Posts: 7
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again for your feedback. I have another question for you since you seem to be knowlwdgeable on the ListField. I want to use the same ListField for 4-5 different lists rather than creating a sparate list for each one. The code below illustrates what I have done to-date.

Code:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import java.util.*;

class DrilldownScreen extends MainScreen {
   
    private String[] sYears = {"2006","2005", "2004", "2003", "2002"};
    private String[] sMakes = {"Acura","BMW", "Cherolet", "Chrysler", "Dodge"};
    private ListField mainList;
    private ListCallback mainCallback;
    private int iList;
        
    public DrilldownScreen() {
        
        setTitle("Select Year");
        
        iList = 1;
        
        mainList = new ListField();
        mainCallback = new ListCallback();
        
        mainList.setCallback(mainCallback);
        
        loadList(sYears, 5);
        
        this.add(mainList);
                                           
    }
    public void loadList(String str[], int max){
        for (int i=0;i<max;i++) {
            mainList.insert(i);
            mainCallback.insert(str[i].toString(), 0);
        }
    }
    public void clearList(int max){
        for (int i=0;i<max;i++) {
            try {
                mainList.delete(i);
            } catch(Exception e) {
                return;
            }
        }
    }
    public boolean trackwheelClick( int status, int time ) {
                       
        switch(iList)
        {
            case 1: 
                setTitle("Select Make");
                iList = 2;
                
                BBXUsed.iYear = mainList.getSelectedIndex();
                BBXUsed.strYear = (String)mainCallback.get(mainList, BBXUsed.iYear);
                
                clearList(5);
                mainCallback.erase();
                loadList(sMakes, 5);
                
                
                
                break;
            case 2: 
                BBXUsed.iMake = mainList.getSelectedIndex();
                BBXUsed.strMake = (String)mainCallback.get(mainList, BBXUsed.iMake);
                
		dismiss();
                
                
                break;
            
        };
        
        return true;
    }

   
    private void dismiss(){
        
        synchronized(UiApplication.getUiApplication().getEventLock()) {
            UiApplication.getUiApplication().popScreen(DrilldownScreen.this); 
        }  
        
    }
    private static class ListCallback implements ListFieldCallback
    {
        private Vector listElements = new Vector();
        public void drawListRow(ListField list, Graphics g, int index, int y, int w)
        {
            String text = (String)listElements.elementAt(index);
            g.drawText(text, 0, y, 0, w);
        }
        public Object get(ListField list, int index)
        {
            return listElements.elementAt(index);
        }
        public int indexOfList(ListField list, String p, int s)
        {
            return listElements.indexOf(p, s);
        }
        public int getPreferredWidth(ListField list)
        {
            return Graphics.getScreenWidth();
        }
        public void insert(String toInsert, int index)
        {
            listElements.addElement(toInsert);
        }
        public void erase()
        {
            listElements.removeAllElements();
        }
        
    }

}
The problem: After I select an entry from the first list I seem to be having a problem clearing the first list and reseting the pointer for the second list, so it gives me problems in the debugger. In the debugger if i continue to hit the forward key I eventually get my second list, so I think I am close.

Any feedback would be appreciated.

Mike
Offline  
Old 08-18-2005, 03:46 PM   #9 (permalink)
New Member
 
Join Date: Aug 2005
Location: Buford, GA
Model: 7250
Posts: 7
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I figured it out, by using the setSize() method after clearing the ListField it appeared to reset the pointer, so the second list works fine now.
Offline  
Old 08-21-2005, 09:42 PM   #10 (permalink)
Knows Where the Search Button Is
 
timinator's Avatar
 
Join Date: Jul 2005
Location: Washington
Model: 8300
Carrier: AT&T
Posts: 29
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

WTG BBF! Good to see more code postings on the site. I'm hoping to post another example soon myself. Keep up the good work.
__________________
===================================
"There are 10 types of people in the world.
Those that understand binary and those that don't!"
www.timothytrimble.info - The ART of S/W Development
==================================
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.