黑莓果表视图

时间:2009-10-13 15:20:45

标签: user-interface blackberry custom-controls

这是我的应用程序。如何在下面添加表视图或网格。 我应该画每一件事PLZ帮助 这是我的代码

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 net.rim.device.api.util.*;
import java.util.*;


/*An application in which user enters the data. this data is displayed when user press the save button*/



public class Display extends UiApplication {

 /*declaring Strings to store the data of the user*/

 String getFirstName;
 String getLastName;
 String getEmail;
 String getGender;
 String getStatus;

 /*declaring text fields for user input*/
 private AutoTextEditField firstName;

 private AutoTextEditField lastName;

 private EmailAddressEditField email;
/*declaring choice field for user input*/
 private ObjectChoiceField gender;
 /*declaring check box field for user input*/
 private CheckboxField status;
 //Declaring button fields
 private ButtonField save;
 private ButtonField close;
 private ButtonField List;
 /*declaring vector*/
 private static Vector _data;
 /*declaring persistent object*/
 private static PersistentObject store;

 /*creating an entry point*/
public static void main(String[] args) 
{

  Display obj = new Display();
  obj.enterEventDispatcher();



}

/*creating default constructor*/
 public Display() 
 {

  /*Creating an object of the main screen class to use its functionalities*/
  MainScreen mainScreen = new MainScreen();

  //setting title of the main screen
  mainScreen.setTitle(new LabelField("Enter Your Data"));

  //creating text fields for user input
  firstName = new AutoTextEditField("First Name: ", "");
  lastName= new AutoTextEditField("Last Name: ", "");
  email= new EmailAddressEditField("Email:: ", "");

  //creating choice field for user input
  String [] items = {"Male","Female"};
  gender= new ObjectChoiceField("Gender",items);
  //creating Check box field
  status = new CheckboxField("Active",true);
  //creating Button fields and adding functionality using listeners
  save = new ButtonField("Save",ButtonField.CONSUME_CLICK);
  save.setChangeListener(new FieldChangeListener()
  {
   public void fieldChanged(Field field, int context)
   {
    save();

   }
  });
  close = new ButtonField("Close",ButtonField.CONSUME_CLICK);
  close.setChangeListener(new FieldChangeListener()
  {
   public void fieldChanged(Field field, int context)
   {
    onClose();
   }
  });
  List = new ButtonField("List",ButtonField.CONSUME_CLICK);
  List.setChangeListener(new FieldChangeListener()
  {
   public void fieldChanged(Field field, int context){

    pushScreen(new ListScreen());



   }
  });
  //adding the input fields to the main screen
  mainScreen.add(firstName);
  mainScreen.add(lastName);
  mainScreen.add(email);
  mainScreen.add(gender);
  mainScreen.add(status);
  //adding buttons to the main screen
  HorizontalFieldManager horizontal = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
  horizontal.add(close);
  horizontal.add(save);
  horizontal.add(List);
  mainScreen.add(horizontal);

  //adding menu items

  mainScreen.addMenuItem(saveItem);
  mainScreen.addMenuItem(getItem);
  mainScreen.addMenuItem(Deleteall);
  //pushing the main screen
  pushScreen(mainScreen);
 }
 private MenuItem Deleteall = new MenuItem("Delete all",110,10)
 {
  public void run()
  {
   int response = Dialog.ask(Dialog.D_YES_NO,"Are u sure u want to delete entire Database");
   if(Dialog.YES == response){
   PersistentStore.destroyPersistentObject(0xdec6a67096f833cL);
   onClose();
   }
   else
    Dialog.inform("Thank God");
  }
 };
//adding functionality to menu item "saveItem" 
private MenuItem saveItem = new MenuItem("Save", 110, 10) 
{

  public void run() 
  {
   //Calling save method
   save();
  }
};
//adding functionality to menu item "saveItem" 
private MenuItem getItem = new MenuItem("Get", 110, 11) 
{
 //running thread for this menu item
 public void run() 
 {


   //synchronizing thread
   synchronized (store) 
   {
    //getting contents of the persistent object

    _data = (Vector) store.getContents();
    try{

     for (int i = _data.size()-1; i >-1; i--) 
     {

      StoreInfo info = (StoreInfo)_data.elementAt(i);
      //checking for empty object
      if (!_data.isEmpty()) 
      {
      //if not empty
      //create a new object of Store Info class






      //storing information retrieved in strings
      getFirstName = (info.getElement(StoreInfo.NAME));
      getLastName  = (info.getElement(StoreInfo.LastNAME));
      getEmail   = (info.getElement(StoreInfo.EMail));
      getGender   =  (info.getElement(StoreInfo.GenDer));
      getStatus  = (info.getElement(StoreInfo.setStatus));

       //calling the show method
      show();
      }

     }
    }
    catch(Exception e){}
   } 
  }


};
public void save()
{



 //creating an object of inner class StoreInfo
 StoreInfo info = new StoreInfo();
 //getting the test entered in the input fields
 info.setElement(StoreInfo.NAME, firstName.getText());
 info.setElement(StoreInfo.LastNAME,lastName.getText());
 info.setElement(StoreInfo.EMail, email.getText());
 info.setElement(StoreInfo.GenDer,gender.toString());
 if(status.getChecked())
  info.setElement(StoreInfo.setStatus, "Active");
 else
  info.setElement(StoreInfo.setStatus, "In Active");
 //adding the object to the end of the vector
 _data.addElement(info);
 //synchronizing the thread
 synchronized (store) 
 {

  store.setContents(_data);
  store.commit();



 }
 //resetting the input fields

 Dialog.inform("Success!");
 firstName.setText(null);
 lastName.setText(null);
 email.setText("");
 gender.setSelectedIndex("Male");
 status.setChecked(true);


}
//coding for persistent store
static {

store =
PersistentStore.getPersistentObject(0xdec6a67096f833cL);
synchronized (store) {
if (store.getContents() == null) {
store.setContents(new Vector());
store.commit();
}
}
_data = new Vector();
_data = (Vector) store.getContents();

}
//new class store info implementing persistable
private static final class StoreInfo implements Persistable 
{
 //declaring variables
 private Vector _elements;
 public static final int NAME = 0;
 public static final int LastNAME = 1;
 public static final int EMail= 2;
 public static final int GenDer = 3;
 public static final int setStatus = 4;

 public StoreInfo() 
 {
  _elements = new Vector(5);
  for (int i = 0; i < _elements.capacity(); ++i) 
  {
   _elements.addElement(new String(""));
  }
 }

 public String getElement(int id) 
 {
  return (String) _elements.elementAt(id);
 }
 public void setElement(int id, String value) 
 {
  _elements.setElementAt(value, id);
 }
}
//details for show method
public void show()
{
 Dialog.alert("Name is "+getFirstName+" "+getLastName+"\nGender is "+getGender+"\nE-mail: "+getEmail+"\nStatus is "+getStatus);
}
public void list()
{

 Dialog.alert("haha");


}

//creating save method

//overriding onClose method

public boolean onClose()
{


 System.exit(0);
 return true;
}

 class ListScreen extends MainScreen 
{


  String firstUserName="Ali";
  String lastUserName="Asif";
  String userEmail="assad";
  String userGender="asdasd";
  String userStatus="active";
  private AutoTextEditField  userFirstName;
  private AutoTextEditField  userLastName;
  private EmailAddressEditField  userMail;
  private ObjectChoiceField  usersGender;
  private CheckboxField usersStatus; 
  private ButtonField btnBack;

 public ListScreen()
 {
  SeparatorField sps = new SeparatorField();
  HorizontalFieldManager hr = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER|HorizontalFieldManager.HORIZONTAL_SCROLLBAR);
  VerticalFieldManager vr = new VerticalFieldManager();
  setTitle(new LabelField("List of All Data"));
  list();
  btnBack = new ButtonField("Back",ButtonField.CONSUME_CLICK);
  btnBack.setChangeListener(new FieldChangeListener()
  {
   public void fieldChanged(Field field,int context)
   {
    UiApplication.getUiApplication().popScreen(getScreen());
   }
  });

  hr.add(btnBack);



  add(hr);
  add(sps);

 }
public void list()

 {
 _data = (Vector) store.getContents();
 try{
  int sn=0;
  for (int i = _data.size()-1; i >-1; i--,sn++) 
  {

   StoreInfo info = (StoreInfo)_data.elementAt(i);
   //checking for empty object
   if (!_data.isEmpty()) 
   {
   //if not empty
   //create a new object of Store Info class






   //storing information retrieved in strings
    firstUserName = (info.getElement(StoreInfo.NAME));
    lastUserName = (info.getElement(StoreInfo.LastNAME));
    userEmail  = (info.getElement(StoreInfo.EMail));
    userGender   =  (info.getElement(StoreInfo.GenDer));
    userStatus  = (info.getElement(StoreInfo.setStatus));

    //calling the listAll method
   listAll();
   }

  }
 }
 catch(Exception e){}

 }
 public void listAll()
 {
  SeparatorField sp = new SeparatorField();
  SeparatorField sps = new SeparatorField();
  HorizontalFieldManager hrs = new HorizontalFieldManager(HorizontalFieldManager.HORIZONTAL_SCROLL);
  hrs.add(new RichTextField(""+firstUserName+" "+lastUserName+" | "+userEmail+" | "+userGender+" | "+userStatus));
  //add(new RichTextField("Email: "+userEmail));
  //add(new RichTextField("Gender: "+userGender));
  //add(new RichTextField("Status: "+userStatus));
  //SeparatorField sp = new SeparatorField();
  add(hrs);
  add(sp);
  add(sps);
 }

 public boolean onClose()
 {
  System.exit(0);
  return true;
 }

}
}

1 个答案:

答案 0 :(得分:3)

Anthony Rizk有一个很好的GridFieldManager

代码:

    public void list()
    {
        if (null != mGrid && null != mGrid.getManager())
            mGrid.getManager().delete(mGrid);
        int colWidth = net.rim.device.api.system.Display.getWidth() / 4; 
        mGrid = new GridFieldManager(new int[] { 0, colWidth, colWidth,
                colWidth, colWidth }, VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
        mGrid.add(new NullField(FOCUSABLE));
        mGrid.add(new LabelField("Name"));
        mGrid.add(new LabelField("E-Mail"));
        mGrid.add(new LabelField("Gender"));
        mGrid.add(new LabelField("Active"));
        add(mGrid);
        _data = (Vector) store.getContents();
        try {
            int sn = 0;
            for (int i = _data.size() - 1; i > -1; i--, sn++) {

                StoreInfo info = (StoreInfo) _data.elementAt(i);
                // checking for empty object
                if (!_data.isEmpty()) {
                    // if not empty
                    // create a new object of Store Info class

                    // storing information retrieved in strings
                    firstUserName = (info.getElement(StoreInfo.NAME));
                    lastUserName = (info.getElement(StoreInfo.LastNAME));
                    userEmail = (info.getElement(StoreInfo.EMail));
                    userGender = (info.getElement(StoreInfo.GenDer));
                    userStatus = (info.getElement(StoreInfo.setStatus));

                    // calling the listAll method
                    mGrid.add(new NullField(FOCUSABLE));
                    mGrid.add(new LabelField(firstUserName + " "
                            + lastUserName));
                    mGrid.add(new LabelField(userEmail));
                    mGrid.add(new LabelField(userGender));
                    mGrid.add(new LabelField(userStatus));
                }

            }
        } catch (Exception e) {
        }

    }

另见 BlackBerry Grid Layout Manager updated