【问题标题】:List field in blackberry , event click listener not working黑莓中的列表字段,事件单击侦听器不起作用
【发布时间】:2013-05-06 05:36:51
【问题描述】:

我正在开发 BB OS v5.0。我设法让列表出现在屏幕上。我正在从 web 服务获取数据并将其添加到 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);
    }   

【问题讨论】:

  • 请发布您的CustomListField4 课程中的一些代码。我们不知道那是什么。我看不出上面有什么问题,除非是 CustomListField4 中的内容,或者您​​在联系人列表中查找 index 的方式。
  • @Signare,我没有 100% 理解你的评论。你认为fetchAlbumsForLetter() 是由于单击按钮而被调用的吗?
  • @user2291839,您的CustomListField4 继承自什么? extend是什么类?
  • @Nate 类 CustomListField4 扩展 ListField 实现 ListFieldCallback
  • @Nate 你知道任何解决方案吗?

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


【解决方案1】:

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

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

【讨论】:

  • 我将列表添加到按钮单击内的垂直字段管理器中。所以当我调试时,我得到了 Field f = getLeafFieldWithFocus();。但我没有得到 f instanceof ListField。 f 不是列表字段的实例。这就是我得到的错误。
  • @Signare 希望您了解getFieldWithFocus()getLeafFieldWithFocus() 之间的区别。第一个函数将为您提供“包含”焦点的屏幕的直接子字段 - 在您的情况下为垂直字段管理器。使用第二个函数将为您提供实际具有焦点的字段对象。
  • 你得到的字段类型是什么?尝试对字段的类名进行系统输出 - f.getClass.getName()
  • 还要确保您访问的是屏幕的getLeafFieldWithFocus() 而不是其他管理员。如果您的 getValue4() 方法在某个管理器中定义,这是可能的。如果在经理内做,你可以尝试做getScreen().getLeafFieldWithFocus()
  • f.getClass().getName() 无效是什么意思?
【解决方案2】:

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

这在您发布的原始代码中并不明显,但通过查看您的更新,我假设您为每一行调用一次 fetchAlbumsForLetter()。这是不对的。

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

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

我会做以下两件事之一:

1。继续使用 ListField

如果您希望 CustomListField4 成为 ListField (extends ListField),那么在您的实现中

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

您实际上应该绘制图形对象,使用所有Graphics#draw 方法。这些是原始图形项目,如填充区域、线条、文本或位图。你会 not be using Field objects inside each ListField row,就像你尝试使用你的 TableRowManager 类一样。

See here for a sample ListField,或here 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() 的方式可以很好地检测行点击,并对选定的行执行某些操作。

【讨论】:

  • 当我直接在我的主屏幕中添加相同的listfield5时,没有问题发生。当我将列表添加到字段管理器时会出现问题。
  • 如果 (f instanceof ListField) 在这种情况下不是真的,因为我将列表添加到字段管理器。
  • @Signare,这个问题或答案中没有listfield5,所以我不确定你指的是什么。我的建议是完全重写这段代码,因为它错误地使用了字段和管理器对象。如果您尝试此操作,但仍有问题,请使用您正在使用的新代码发布一个新问题。
  • @Signare,同样,如果您将CustomListField4 更改为ListField 继承,那么检查f instanceof ListField 将始终为假。但是,我不确定为什么该检查仍然存在。我认为你根本不需要if (f instanceof。除非getValue4() 也被另一个navigationClick() 方法调用,否则它总是会因为单击CustomListField4 而被调用,对吧?所以,我认为他们不需要那张支票。
【解决方案3】:

你可以试试这个

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

【讨论】:

  • getValue4() 已经在 UI 线程上运行,所以很遗憾,我认为这不会解决问题。
  • 啊,你是对的。因为我在无法显示对话框的时候使用了这个方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-08
相关资源
最近更新 更多