Listview 黑莓:复选框和列表视图
我再次陷入了用复选框实现列表视图的困境。我必须在带有复选框的列表视图中显示blackberry联系人。当我运行代码时,它正在消耗额外的内存。我发布的代码中没有显示任何内容。请告诉我我在哪里犯了错误,以及如何纠正错误 更新: 代码现在正在运行,但在滚动时将数组抛出绑定异常。你能告诉我问题在哪里吗Listview 黑莓:复选框和列表视图,listview,blackberry,Listview,Blackberry,我再次陷入了用复选框实现列表视图的困境。我必须在带有复选框的列表视图中显示blackberry联系人。当我运行代码时,它正在消耗额外的内存。我发布的代码中没有显示任何内容。请告诉我我在哪里犯了错误,以及如何纠正错误 更新: 代码现在正在运行,但在滚动时将数组抛出绑定异常。你能告诉我问题在哪里吗 public class CheckboxListField extends MainScreen implements ListFieldCallback { private Vect
public class CheckboxListField extends MainScreen implements ListFieldCallback {
private Vector _listData = new Vector();
private ListField listField;
private ContactList blackBerryContactList;
private BlackBerryContact blackBerryContact;
private Vector blackBerryContacts;
private class ChecklistData
{
private String _stringVal;
private boolean _checked;
ChecklistData(String stringVal, boolean checked)
{
_stringVal = stringVal;
_checked = checked;
}
//Get/set methods.
private String getStringVal()
{
return _stringVal;
}
private boolean isChecked()
{
return _checked;
}
}
CheckboxListField()
{
listField = new ListField();
listField.setCallback(this);
reloadContactList();
for(int count = 0; count < blackBerryContacts.size(); ++count)
{
BlackBerryContact item =
(BlackBerryContact)blackBerryContacts.elementAt(count);
String displayName = getDisplayName(item);
add(new RichTextField("Check1"));
_listData.addElement(new ChecklistData(displayName, false));
add(new RichTextField("Check2"));
listField.insert(count);
add(new RichTextField("Check3"));
add(new RichTextField(blackBerryContacts.size()));
add(new RichTextField(displayName));
}
add(listField);
}
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;
}
}
//Convert the list of contacts from an Enumeration to a Vector
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 list, Graphics graphics, int index, int y, int w)
{
ChecklistData currentRow = (ChecklistData)this.get(list, index);
StringBuffer rowString = new StringBuffer();
if (currentRow.isChecked())
{
rowString.append(Characters.BALLOT_BOX_WITH_CHECK);
}
else
{
rowString.append(Characters.BALLOT_BOX);
}
//Append a couple spaces and the row's text.
rowString.append(Characters.SPACE);
rowString.append(Characters.SPACE);
rowString.append(currentRow.getStringVal());
//Draw the text.
graphics.drawText(rowString.toString(), 0, y, 0, w);
}
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;
}
if (displayName != null) {
final String namePrefix = name[Contact.NAME_PREFIX];
if (namePrefix != null) {
displayName = namePrefix + " " + displayName;
}
return displayName;
}
}
return displayName;
}
//Returns the object at the specified index.
public Object get(ListField list, int index)
{
return _listData.elementAt(index);
}
//Returns the first occurence of the given String, bbeginning the search at index,
//and testing for equality using the equals method.
public int indexOfList(ListField list, String p, int s)
{
return -1;
}
//Returns the screen width so the list uses the entire screen width.
public int getPreferredWidth(ListField list)
{
return Display.getWidth();
}
}
public类CheckboxListField扩展MainScreen实现ListFieldCallback{
私有向量_listData=新向量();
私有ListField-ListField;
私人通讯录黑莓通讯录;
私人BlackBerryContact BlackBerryContact;
专用向量blackBerryContacts;
私有类ChecklistData
{
私有字符串_stringVal;
私有布尔值被选中;
ChecklistData(字符串stringVal,布尔值已选中)
{
_stringVal=stringVal;
_已检查=已检查;
}
//获取/设置方法。
私有字符串getStringVal()
{
返回_stringVal;
}
私有布尔值已检查()
{
返回已检查;
}
}
CheckboxListField()
{
listField=新listField();
setCallback(this);
重新加载ContactList();
对于(int count=0;count0){
最终字符串[]名称=contact.getStringArray(contact.name,0);
最后一个字符串firstName=name[联系人姓名];
最后一个字符串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;
}
if(displayName!=null){
最终字符串namePrefix=姓名[联系人姓名前缀];
如果(名称前缀!=null){
displayName=namePrefix+“”+displayName;
}
返回显示名;
}
}
返回显示名;
}
//返回指定索引处的对象。
公共对象获取(列表字段列表,int索引)
{
返回_listData.elementAt(索引);
}
//返回给定字符串的第一次出现,b在索引处开始搜索,
//以及使用equals方法测试相等性。
public int indexOfList(列表字段列表,字符串p,int s)
{
返回-1;
}
//返回屏幕宽度,以便列表使用整个屏幕宽度。
public int getPreferredWidth(列表字段列表)
{
返回Display.getWidth();
}
}
创建自定义列表后。将其添加到VerticalFieldMan中
int DISPLAY_WIDTH = Display.getWidth();
int DISPLAY_HEIGHT = Display.getHeight();
VerticalFieldManager VFM mListManager = new SizedVFM(DISPLAY_WIDTH, DISPLAY_HEIGHT - 40);
mListManager.add(mListField);
add(mListManager);