黑莓:在列表中添加复选框
我已成功获取联系人列表。但我无法在该列表中添加复选框。我已经从复选框及其工作中制作了单独的程序。但不包括联系人列表。有人可以告诉我应该在哪里添加复选框吗?这是代码:
public final class ContactsScreen extends MainScreen implements ListFieldCallback {
private ListField listField;
private ContactList blackBerryContactList;
private Vector blackBerryContacts;
public ContactsScreen(){
CheckboxField checkBox1 = new CheckboxField();
setTitle(new LabelField( "Contacts", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ));
listField = new ListField();
listField.setCallback(this);
add(listField);
add(new RichTextField("Size" +(listField)));
reloadContactList();
}
private boolean reloadContactList() {
try {
blackBerryContactList = (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
Enumeration allContacts = blackBerryContactList.items();
blackBerryContacts = enumToVector(allContacts);
listField.setSize(blackBerryContacts.size());
return true;
}
catch(PIMException e){
return false;
}
}
private Vector enumToVector(Enumeration contactEnum) {
Vector v = new Vector();
if (contactEnum == null)
return v;
while (contactEnum.hasMoreElements()) {
v.addElement(contactEnum.nextElement());
}
return v;
}
public void drawListRow(ListField fieldVar, Graphics graphics, int index, int y, int width){
if ( listField == fieldVar && index < blackBerryContacts.size())
{
add(new RichTextField(blackBerryContacts.size()));
BlackBerryContact item = (BlackBerryContact)blackBerryContacts.elementAt(index);
String displayName = getDisplayName(item);
graphics.drawText(displayName, 0, y, 0, width);
}
}
public Object get(ListField fieldVar, int index)
{
if (listField == fieldVar) {
return blackBerryContacts.elementAt(index);
}
return null;
}
public int getPreferredWidth(ListField fieldVar ) {
return Display.getWidth();
}
public int indexOfList(ListField fieldVar, String prefix, int start)
{
return -1; // not implemented
}
public static String getDisplayName(Contact contact) {
if (contact == null) {
return null; }
String displayName = null;
// First, see if there is a meaningful name set for the contact.
if (contact.countValues(Contact.NAME) > 0) {
final String[] name = contact.getStringArray(Contact.NAME, 0);
final String firstName = name[Contact.NAME_GIVEN];
final String lastName = name[Contact.NAME_FAMILY];
if (firstName != null && lastName != null) {
displayName = firstName + " " + lastName;
} else if (firstName != null) {
displayName = firstName;
} else if (lastName != null) {
displayName = lastName;
}
} return displayName;
}
}
I have fetched contact list successfully. But I am not able to add check boxes with that list. I have made separate program from checkbox and its working. but not with the contact list. Can anybody tell me here where should I add checkboxes? Here is the code:
public final class ContactsScreen extends MainScreen implements ListFieldCallback {
private ListField listField;
private ContactList blackBerryContactList;
private Vector blackBerryContacts;
public ContactsScreen(){
CheckboxField checkBox1 = new CheckboxField();
setTitle(new LabelField( "Contacts", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ));
listField = new ListField();
listField.setCallback(this);
add(listField);
add(new RichTextField("Size" +(listField)));
reloadContactList();
}
private boolean reloadContactList() {
try {
blackBerryContactList = (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
Enumeration allContacts = blackBerryContactList.items();
blackBerryContacts = enumToVector(allContacts);
listField.setSize(blackBerryContacts.size());
return true;
}
catch(PIMException e){
return false;
}
}
private Vector enumToVector(Enumeration contactEnum) {
Vector v = new Vector();
if (contactEnum == null)
return v;
while (contactEnum.hasMoreElements()) {
v.addElement(contactEnum.nextElement());
}
return v;
}
public void drawListRow(ListField fieldVar, Graphics graphics, int index, int y, int width){
if ( listField == fieldVar && index < blackBerryContacts.size())
{
add(new RichTextField(blackBerryContacts.size()));
BlackBerryContact item = (BlackBerryContact)blackBerryContacts.elementAt(index);
String displayName = getDisplayName(item);
graphics.drawText(displayName, 0, y, 0, width);
}
}
public Object get(ListField fieldVar, int index)
{
if (listField == fieldVar) {
return blackBerryContacts.elementAt(index);
}
return null;
}
public int getPreferredWidth(ListField fieldVar ) {
return Display.getWidth();
}
public int indexOfList(ListField fieldVar, String prefix, int start)
{
return -1; // not implemented
}
public static String getDisplayName(Contact contact) {
if (contact == null) {
return null; }
String displayName = null;
// First, see if there is a meaningful name set for the contact.
if (contact.countValues(Contact.NAME) > 0) {
final String[] name = contact.getStringArray(Contact.NAME, 0);
final String firstName = name[Contact.NAME_GIVEN];
final String lastName = name[Contact.NAME_FAMILY];
if (firstName != null && lastName != null) {
displayName = firstName + " " + lastName;
} else if (firstName != null) {
displayName = firstName;
} else if (lastName != null) {
displayName = lastName;
}
} return displayName;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ListField
不是为此设计的。它的列表项不是Manager
,因此您无法向其中添加任何子字段。换句话说,这对于 BB 上的ListField
来说是不可能的。ListField
是一种在 UI 上表示长列表的方法,而不会占用太多 RAM(因为在本例中只有唯一的 UI 对象 -ListField
)。如果您的列表不太长(10 - 20 项),请考虑使用
VerticalFieldManager
而不是ListField
。如果列表很长&&您确实需要复选框,然后考虑使用VerticalFieldManager
+ 列表分页。ListField
is not designed for this. Its list item is not aManager
, so you can't add any child fields to it. In other words this is not possible withListField
on BB.ListField
is a way to represent on UI a long list without eating too much RAM (since in this case there is the only UI object - theListField
).If your list is not too long (10 - 20 items) then consider using
VerticalFieldManager
instead ofListField
. If list is long && you really need check boxes on it then consider usingVerticalFieldManager
+ list pagination.