/**
* Demonstrate the use of JSR 75 PIM APIs
*/
public class ItemSelectionScreen implements CommandListener{
private final Command selectCommand = new Command("Select", Command.ITEM, 1);
private final Command backCommand = new Command("Back", Command.BACK, 1);
private final PIMDemo midlet;
int listType;
PIMList list;
Thread thread;
List list1;
private final Vector itemList = new Vector();
public ItemSelectionScreen(PIMDemo midlet) throws PIMException {
list1 = new List("Select a list type", List.IMPLICIT);
this.midlet = midlet;
listType = PIM.CONTACT_LIST;
String[] lists = PIM.getInstance().listPIMLists(listType);
for (int i = 0; i < lists.length; i++) {
list1.append(lists[i], null);
}
populateList();
list1.addCommand(backCommand);
list1.setCommandListener(this);
}
void populateList() {
new Thread(new Runnable() {
public void run() {
String listName = list1.getString(list1.getSelectedIndex());
try {
list = PIM.getInstance().openPIMList(listType, PIM.READ_WRITE,listName );
} catch (PIMException ex) {
ex.printStackTrace();
}
synchronized (this) {
try {
for (Enumeration items = list.items(); items.hasMoreElements();) {
PIMItem item = (PIMItem)items.nextElement();
int fieldCode = 0;
String label = getDisplayedField(item);
if (label == null) {
label = "<Incomplete data>";
}
list1.append(label, null);
itemList.addElement(item);
}
if (list1.size() > 0) {
list1.setSelectCommand(selectCommand);
}
} catch (PIMException e) {
e.printStackTrace();
}
}
}
}).start();
}
public void commandAction(Command command, Displayable displayable) {
if (command == selectCommand) {
try {
PIMItem item = (PIMItem)itemList.elementAt(list1.getSelectedIndex());
Displayable screen = new ItemDisplayScreen(midlet, this, item);
Display.getDisplay(midlet).setCurrent(screen);
} catch (Exception e) {
// midlet.reportException(e, this);
}
}
}
int getDisplayedFieldCode() {
int fieldCode = 0;
switch (listType) {
case PIM.CONTACT_LIST:
fieldCode = Contact.FORMATTED_NAME;
break;
}
return fieldCode;
}
void fixDisplayedField(PIMItem item) {
int fieldCode = getDisplayedFieldCode();
if (listType == PIM.CONTACT_LIST) {
boolean defined = false;
if (item.countValues(fieldCode) != 0) {
String s = item.getString(fieldCode, 0);
if ((s != null) && (s.trim().length() > 0)) {
defined = true;
}
}
if (!defined) {
// try to fill in the values from NAME
if (item.countValues(Contact.NAME) != 0) {
String[] a = item.getStringArray(Contact.NAME, 0);
int[] b = item.getFields();
if (a != null) {
StringBuffer sb = new StringBuffer();
// sb.append(b[Contact.TEL]);
if (a[Contact.NAME_GIVEN] != null) {
sb.append(a[Contact.NAME_GIVEN]);
}
if (a[Contact.NAME_FAMILY] == null) {
if (sb.length() > 0) {
sb.append(a[Contact.NAME_FAMILY]);
}
}
String s = sb.toString().trim();
if (s.length() > 0) {
if (item.countValues(fieldCode) == 0) {
item.addString(fieldCode, Contact.ATTR_NONE, s);
} else {
item.setString(fieldCode, 0, Contact.ATTR_NONE, s);
}
}
}
}
}
}
}
String getDisplayedField(PIMItem item) {
int fieldCode = getDisplayedFieldCode();
fixDisplayedField(item);
String fieldValue = null;
if (item.countValues(fieldCode) != 0) {
String s = item.getString(fieldCode, 0);
if ((s != null) && (s.trim().length() != 0)) {
fieldValue = s;
}
}
return fieldValue;
}
}
这个代码,运行的时候 会显示[lists[i]]
for (int i = 0; i < lists.length; i++) {
list1.append(lists[i], null);
}
如果我把这个代码删除,或者只删除list1.append(lists[i], null);
都会出现NULL.POIONT。 原因是String listName = list1.getString(list1.getSelectedIndex());
获取不到,
有没有方法直接显示PIM里面的内容的方法,