Search code examples
blackberrylistfield

How to Re-Arrange ListField in Blackberry


How to re-arrange the items of a ListField? I want to select the item on click on Blackberry (Storm and Torch) Touch Phones. After select I want to (Move and Drag) Scroll item Vertically on another list item(list items scroll vertically in background) or screen. After scrolling, drop item any where on (in list field) Screen.


Solution

  • i am also working on Custom list field. Just Click on listitem. after click U can move (selected) listItem vertically. if u want to place any where in screen than click again on trackpad. when u click it will be place on that position Checkout. here is my code.

    public class ListDemo extends MainScreen implements ListFieldCallback {
    private VerticalFieldManager vfm, vfm_List;
    private ListField myList;
    private Vector vector;
    private String[] str_arr;
    int Height = Display.getHeight();
    int Width = Display.getWidth();
    int i ;
    boolean bool = true;
    int index = 0;
    private boolean hasFocus = false;
    private boolean b = true;
    public static int curSelected ;
    Channel obj; 
    
    public ListDemo() {
        vector = new Vector();
        for (i = 0; i < 8; i++) {
            obj = new Channel();
            if (i == 0) {
                obj.setCh_icon("ajtak.jpg");
                obj.setCh_Name("NightSuite");
                obj.setCh_Move("move.png");
    
            } else if (i == 1) {
                obj.setCh_icon("cnbc.jpg");
                obj.setCh_Name("Shirts");
                obj.setCh_Move("move.png");
    
            } else if (i == 2) {
                obj.setCh_icon("zee.jpg");
                obj.setCh_Name("Jeans");
                obj.setCh_Move("move.png");
    
            } else if (i == 3) {
                obj.setCh_icon("astha.jpg");
                obj.setCh_Name("Bags");
                obj.setCh_Move("move.png");
    
            }else if (i == 4) {
                obj.setCh_icon("fox_news.jpg");
                obj.setCh_Name("Coat");
                obj.setCh_Move("move.png");
    
            }else if (i == 5) {
                obj.setCh_icon("news.jpg");
                obj.setCh_Name("Coat");
                obj.setCh_Move("move.png");
    
            } else if (i == 6) {
                obj.setCh_icon("star_news.jpg");
                obj.setCh_Name("Coat");
                obj.setCh_Move("move.png");
    
            } else {
                obj.setCh_icon("assiant.jpg");
                obj.setCh_Name("Shorts");
                obj.setCh_Move("move.png");
            }
            vector.addElement(obj);
        }
        init();
    }
    
    public void init() {
    
        vfm = new VerticalFieldManager(USE_ALL_HEIGHT | USE_ALL_WIDTH) {
    
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(maxWidth, maxHeight);
                setExtent(Width, Height);
            }
        };
    
        vfm_List = new VerticalFieldManager(VERTICAL_SCROLL
                | VERTICAL_SCROLLBAR) {
            protected void sublayout(int maxHeight, int maxWidth) {
                super.sublayout(maxWidth, maxHeight);
                setExtent(Width, Height);
            }
        };
        myList = new ListField()
        {
            public  void onFocus(int direction){  
              hasFocus = true;
    
            }
            public void onUnfocus()   
            {
                hasFocus = false;  
                super.onUnfocus();  
                invalidate();  
            }  
             public void paint(Graphics g) {
                 if (hasFocus)   
                  {  
                       curSelected = getSelectedIndex(); 
    
    
                  }   
                  else   
                  {  
                      curSelected = -1;  
                  } 
                 super.paint(g);
    
            }
        };
        myList.setCallback(this);
        myList.setRowHeight(50);
        myList.setFocusListener(new FocusChangeListener() {
            public void focusChanged(Field field, int eventType) {
    
                if (bool == false) {
                    index = myList.getSelectedIndex();
                    Channel temp = (Channel) vector
                        .elementAt(index);
                    vector.removeElementAt(index);
                    vector.insertElementAt(temp, curSelected);
                    myList.invalidate();
    
                }
    
            }
    
        });
        for (int i = 0; i < vector.size(); i++) {
            myList.insert(i);
    
        }
        vfm_List.add(myList);
        vfm.add(vfm_List);
        add(vfm);
    
    }
    protected boolean navigationClick(int status, int time) {
        if(bool)
        {
            bool = false;
        }
        else
        {
            bool = true;
        }
    
    
    return true;
    } 
    
    
    public void drawListRow(ListField list, Graphics g, int index, int y, int w) 
    {
        String text = ((Channel) vector.elementAt(index)).getCh_Name();
        Bitmap arrow = Bitmap.getBitmapResource(((Channel) vector.elementAt(index)).getCh_icon());
        if(bool == false)
        {
            if (g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
            {
                Bitmap move = Bitmap.getBitmapResource(((Channel) vector.elementAt(index)).getCh_Move());
                g.drawBitmap(Width - 40, y + 13, 25, 25, move, 0, 0);
    
            }
        }
        g.setBackgroundColor(Color.LIGHTBLUE);
        g.setColor(Color.BLACK);
        g.drawBitmap(0, y + 10, 48, 35, arrow, 0, 0);
        g.drawText(text, 50, y + 18, 0, w);
        g.setColor(Color.WHITE);
        invalidate();
    
    
    }
    
    public Object get(ListField list, int index) {
        return vector.elementAt(index);
    }
    
    public int indexOfList(ListField list, String p, int s) {
        return vector.indexOf(p, s);
    }
    
    public int getPreferredWidth(ListField list) {
        return Graphics.getScreenWidth();
    }
    
    public void insert(String toInsert, int index) {
        vector.addElement(toInsert);
    }
    
    public void erase() {
        vector.removeAllElements();
    }
    

    }

    Here is another Channel Class

    public class Channel {
    
    private String ch_Name;
    private String ch_icon;
    private String ch_move;
    
    public String getCh_Name() {
        return ch_Name;
    }
    public void setCh_Name(String ch_Name) {
        this.ch_Name = ch_Name;
    }
    public String getCh_icon() {
        return ch_icon;
    }
    public void setCh_icon(String ch_icon) {
        this.ch_icon = ch_icon;
    }
    public String getCh_Move() {
        return ch_move;
    }
    public void setCh_Move(String ch_move) {
        this.ch_move = ch_move;
    }
    

    }

    Checkout its working on both touch and nontouch .