在Blackberry的自定义列表字段中设置位置?

时间:2010-12-29 15:38:24

标签: java blackberry

我希望从下到上显示三个列表字段项。我能够显示三个列表字段项,但它们从上到下显示。我已经尝试设置位置,但它无法正常工作。

import java.util.Vector;


import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.ContextMenu;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.Arrays;
import net.rim.device.api.ui.component.ListField;

/**
 * @author Jason Emerick
 */
public class TaskListField extends UiApplication 
{
        //statics ------------------------------------------------------------------
        public static void main(String[] args)
        {
            TaskListField theApp = new TaskListField();
            theApp.enterEventDispatcher();
        }
        public TaskListField()
        {
            pushScreen(new TaskList());
        }

}
/*class List extends FullScreen {
    TaskList tl;
    List(){
     super();
    TaskList tl=new TaskList();
    }
}*/
class TaskList extends MainScreen implements ListFieldCallback {
     private Vector rows;
     private Bitmap p1;
     private Bitmap p2;
     private Bitmap p3;
     String Task;
     ListField listnew=new ListField();
     public TaskList() {
      super();
      listnew.setRowHeight(50);
      //setEmptyString("Hooray, no tasks here!", DrawStyle.HCENTER);
      listnew.setCallback(this);

      p1 = Bitmap.getBitmapResource("1.png");
      p2 = Bitmap.getBitmapResource("2.png");
      p3 = Bitmap.getBitmapResource("3.png");

      rows = new Vector();

      for (int x = 0; x < 3; x++) {
       TableRowManager row = new TableRowManager();
       if (x== 0) {
            Task="On Air Now";
        }
       if (x== 1) {
            Task="Music Channel";
        }
       if (x==2) {
            Task="News Channel";
        }
       // SET THE PRIORITY BITMAP FIELD
       // if high priority, display p1 bitmap
       if (x % 2 == 0) {
        row.add(new BitmapField(p1));

       }

       // if priority is 2, set p2 bitmap
       else if (x % 3 == 0) {
        row.add(new BitmapField(p2));

       }
       // if priority is 3, set p3 bitmap
       else {
        row.add(new BitmapField(p3));

       }

       // SET THE TASK NAME LABELFIELD
       // if overdue, bold/underline
       LabelField task = new LabelField(Task,
         DrawStyle.ELLIPSIS);

       // if due today, bold
       if (x % 2 == 0) {
        task.setFont(Font.getDefault().derive(
          Font.BOLD));

       } else {
        task.setFont(Font.getDefault().derive(Font.BOLD));

       }

       row.add(task);

       LabelField task1 = new LabelField("Now Playing" + String.valueOf(x),
                 DrawStyle.ELLIPSIS);

               // if due today, bold
              /* if (x % 2 == 0) {
                task.setFont(Font.getDefault().derive(
                  Font.BOLD));

               } else {
                task.setFont(Font.getDefault().derive(Font.BOLD));

               }*/
               Font myFont = Font.getDefault().derive(Font.PLAIN, 12);
               task1.setFont(myFont);

               row.add(task1);


       // SET THE DUE DATE/TIME
      row.add(new LabelField("",
         DrawStyle.ELLIPSIS | LabelField.USE_ALL_WIDTH
           | DrawStyle.RIGHT) {
        protected void paint(Graphics graphics) {
         graphics.setColor(0x00878787);
         super.paint(graphics);
        }
       });
       rows.addElement(row);
      }

      listnew.setSize(rows.size());
      this.add(listnew);
     }

     // ListFieldCallback Implementation
     public void drawListRow(ListField listField, Graphics g, int index, int y,
       int width) {
      //TaskList list =(TaskListField) listnew;
    TableRowManager rowManager = (TableRowManager)rows
        .elementAt(index);
      rowManager.drawRow(g, 0, y, width, listnew.getRowHeight());
     }

     private class TableRowManager extends Manager {
      public TableRowManager() {
       super(0);
      }

      // Causes the fields within this row manager to be layed out then
      // painted.
      public void drawRow(Graphics g, int x, int y, int width, int height) {
       // Arrange the cell fields within this row manager.
       layout(0, 1);

       // Place this row manager within its enclosing list.
       setPosition(x,y);

       // Apply a translating/clipping transformation to the graphics
       // context so that this row paints in the right area.
      g.pushRegion(getExtent());

       // Paint this manager's controlled fields.
       subpaint(g);

       g.setColor(0x00CACACA);
       g.drawLine(0, 0, getPreferredWidth(), 0);

       // Restore the graphics context.
       g.popContext();
      }

      // Arrages this manager's controlled fields from left to right within
      // the enclosing table's columns.
      protected void sublayout(int width, int height) {
       // set the size and position of each field.
       int fontHeight = Font.getDefault().getHeight();
       int preferredWidth = getPreferredWidth();

       // start with the Bitmap Field of the priority icon
      /* Field field = getField(0);
       layoutChild(field, 0, 0);
       setPositionChild(field, 150, 300);*/

       // set the task name label field
    /*   field = getField(1);
       layoutChild(field, preferredWidth - 16, fontHeight + 1);
       setPositionChild(field, 34, 3);

       // set the list name label field
       field = getField(2);
       layoutChild(field, 150, fontHeight + 1);
       setPositionChild(field, 34, fontHeight + 6);*/

       // set the due time name label field
/*     field = getField(3);
       layoutChild(field, 150, fontHeight + 1);
       setPositionChild(field,4,340);*/

    /* layoutChild(listnew, preferredWidth, fontHeight);
        setPositionChild(listnew, 3, 396);*/
      setExtent(360, 480);

      }

      // The preferred width of a row is defined by the list renderer.
      public int getPreferredWidth() {
       return getWidth();
      }

      // The preferred height of a row is the "row height" as defined in the
      // enclosing list.
      public int getPreferredHeight() {
       return listnew.getRowHeight();
      }
     }

     public Object get(ListField listField, int index) {
      // TODO Auto-generated method stub
      return null;
     }

     public int getPreferredWidth(ListField listField) {
      // TODO Auto-generated method stub
      return 0;
     }

     public int indexOfList(ListField listField, String prefix, int start) {
      // TODO Auto-generated method stub
      return 0;
     }

    }

我需要修复什么才能让它按照我想要的方式工作?

1 个答案:

答案 0 :(得分:0)

我猜您想知道如何将焦点更改为列表字段中的第3项

所以答案是:

listnew.setSelectedIndex(2);