【问题标题】:How to attach and get particular list item id in BlackBerry?如何在 BlackBerry 中附加和获取特定的列表项 ID?
【发布时间】:2023-03-31 10:39:01
【问题描述】:
我正在尝试使用 FieldManager(水平和垂直)创建列表。在这个列表中,我有多个可点击的项目,比如按钮,所以我没有使用 ListField 或 ObjectListField。
我已成功创建 UI,但无法附加来自服务器的特定项目 ID。此外,在单击任何列表行中的特定按钮时,我想获取项目 ID 并希望针对该 ID 执行操作。
所以,请告诉我如何在使用 FieldManager 时将 ID 附加到特定行,然后如何在单击按钮时针对该 ID 生成事件?
【问题讨论】:
标签:
blackberry
blackberry-simulator
blackberry-eclipse-plugin
blackberry-jde
【解决方案1】:
当您创建一行时,您可能正在为每一行创建一个Manager(的子类)。
至少,您似乎在每一行都创建了一个ButtonField。
您可以做的是在创建时将 cookie 附加到每一行或每个按钮。 cookie 只是附加到对象的额外信息。然后,当单击该行或按钮时,您会向该行/按钮询问 cookie,并使用它来识别行 ID。
每个 BlackBerry Field 都可以附加一个 cookie。由于 cookie 的类型为 Object,因此您可以随意制作。
例如,在为行创建按钮时:
for (int i = 0; i < numRows; i++) {
BitmapButtonField button = new BitmapButtonField(onImage, offImage, ButtonField.CONSUME_CLICK);
// use the row index as the cookie
button.setCookie(new Integer(i));
button.setChangeListener(this);
Manager row = new MyRowManager();
row.add(button);
add(row);
}
然后当按钮被点击时:
void fieldChanged(Field field, int context) {
Object cookie = field.getCookie();
if (cookie instanceof Integer) {
Integer rowId = (Integer)cookie;
System.out.println("Row Id = " + rowId);
}
}
注意:我为此使用 BlackBerry Advanced UI BitmapButtonField,但 cookie 技术适用于任何 Field 或 Manager 类。 See another example here.