黑莓中的列表字段,事件点击监听器无法正常工作

时间:2013-05-06 05:36:52

标签: blackberry blackberry-eclipse-plugin blackberry-jde

我正在研发BB OS v5.0。我设法让列表出现在屏幕上。我从webservice获取数据并将其添加到Vector

现在我想找出onclick,这是点击的项目并相应地执行一些操作。为此,我试图显示警报。但我没有收到警报。

这是我的代码:

在我的主屏幕中,我添加了fieldmanager=new VerticalFieldManager();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+"");
    }
}

请帮我解决此问题

修改

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);
    }   

3 个答案:

答案 0 :(得分:1)

您正在调用getFieldWithFocus(),它将为您提供经理。你需要得到叶子领域

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

答案 1 :(得分:0)

我认为您的FieldManager对象的层次结构不正确,这会导致您检测到字段焦点/选择时出现问题。

从您发布的原始代码中看不出来,但通过查看您的更新,我假设您为每行调用fetchAlbumsForLetter()一次。那不对。

fetchAlbumsForLetter()每次调用时都会创建一个新的CustomListField4。而CustomListField4ListField

ListField并不仅仅代表一个行。它意味着代表所有行。您应该只创建一个CustomListField4的实例。

我会做以下两件事之一:

1。继续使用ListField

如果您希望CustomListField4成为ListFieldextends ListField),那么请执行

public void drawListRow(ListField listField, Graphics g, int index, int y, int width);

您应该使用所有Graphics#draw方法绘制图形对象。这些是原始图形项,如填充区域,线条,文本或位图。当您尝试使用TableRowManager课程时,您会not be using Field objects inside each ListField row

See here for a sample ListFieldhere for a more sophisticated example

2。使用管理器模拟ListField

将您的代码更改为

public class CustomListField4 extends VerticalFieldManager {

public class CustomListField4 extends Manager {

然后,您可以为每一行使用TableRowManager,并向其添加LabelFieldBitmapField个对象。

See here for an example of this


如果您解决了这些问题,那么我认为您覆盖navigationClick()的方式可以很好地检测行点击,并对所选行执行某些操作。

答案 2 :(得分:-1)

你可以试试这个

 UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    Dialog.alert(id+"");
                }
            });