Search code examples
blackberryblackberry-eclipse-pluginblackberry-jde

List field in blackberry , event click listener not working


I am working on BB OS v5.0. I have managed to get the list to appear on the screen. I am getting data from webservice and adding it into a Vector.

Now I want to find out onclick, which is the item that is clicked and accordingly perform some operation. For that i am trying to display an alert. But I'm not getting the alert.

Here is my code :

In my mainscreen , i added fieldmanager=new VerticalFieldManager(); and add(fieldmanager);

void fetchAlbumsForLetter(String letter) {
    Status.show("Processing ....", 3000);
    fieldManager.deleteAll();

    VerticalFieldManager top = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR | Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR | Field.USE_ALL_WIDTH){
        public void paint(Graphics graphics) {
            graphics.setBackgroundColor(0x00290008);
            graphics.setColor(Color.WHITE);
            graphics.clear();
            graphics.drawBitmap(0, 0, sha.getWidth(),
            sha.getHeight(), sha, 0, 0);
            super.paint(graphics);
        }
    };
    add(top);

    CustomListField4 list4 = new CustomListField4(null){
        protected boolean navigationClick(int status, int time) {
            getValue4();
            return true;
        }
    };
    fieldmanager.add(list4);
}

protected void getValue4() {
    Field f = getFieldWithFocus();
    if (f instanceof ListField) {
        ListField l = (ListField) f;
        final int index = l.getSelectedIndex();
        HistoryItem _contactslist = (HistoryItem) CustomListField4.val4.elementAt(index);
        final String id = _contactslist.getName();
        Dialog.alert(id+"");
    }
}

Please help me to resolve this

EDIT

class CustomListField4 extends ListField implements ListFieldCallback {

    public CustomListField4(Vector data) {
        super(0, ListField.MULTI_SELECT);
        final TableRowManager row = new TableRowManager() {
            public void paint(Graphics g) {
                // g.fillRect(0, 0, getWidth(), getHeight());
                g.setColor(0x0f3e19b);
                g.clear();
                super.paint(g);
            }
        };

        Bitmap icon = Bitmap.getBitmapResource("Devil Skype.png");
        HorizontalFieldManager h=new HorizontalFieldManager();
        h.add(new BitmapField(icon));
        //h.add(new BitmapField(song.getThumb()));
        h.add(new LabelField(song.getAlbumName()));
        //h.add(new LabelField(row1.getLanguage()));
        //h.setMargin(0,0,50,0);
        //Dialog.alert(song.getName());

        VerticalFieldManager vfm=new VerticalFieldManager();
        vfm.add(h);
        //vfm.add(new LabelField(song.getArtist()));
        row.add(vfm);
        contacts.addElement(row);
    }

    setSize( contacts.size());
}

// ListFieldCallback Implementation
public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
    listField.setRowHeight(index,107);
    CustomListField4 list = (CustomListField4) listField;
    TableRowManager rowManager = (TableRowManager) CustomListField4.contacts.elementAt(index);
    rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}

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

Solution

  • You are calling getFieldWithFocus() which will give you the manager. You need to get the leaf field

    protected void getValue4() {
       Field f = getLeafFieldWithFocus();
       if (f instanceof ListField) {
           //Your code
       }
    }