我使用了列表渲染器,一切正常。然后我保留了最后单击的项目的摘录,如下所示: 渲染器中有 3 个按钮。按钮1在所有情况下都有效,但是按钮2和按钮3在某些情况下工作而在其他情况下不起作用?为什么?
如果我从下面的代码中删除 if (lastClickedButton != null)
,它会为之前在其他列表项中工作的同一按钮给出 NullPointerException
。
我的代码:
@Override
protected boolean initListModelListEmergencyList(List cmp) {
cmp.setModel(new com.codename1.ui.list.DefaultListModel(emergencyPoliceArray));
cmp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
for (int i = 0; i < emergencyPoliceArray.size(); i++) {
if (cmp.getSelectedItem() == emergencyPoliceArray.get(i)) {
lastClickedButton = ((GenericListCellRenderer) cmp.getRenderer()).extractLastClickedComponent();
if (lastClickedButton != null) {
System.out.println("Phn no: " + lastClickedButton.getText());
}
}
}
}
}
);
return true;
}
我的连接:
private void showEmergencyDetails() {
JSONParser p = new JSONParser();
Hashtable<String, Object> test;
try {
test = p.parse(new InputStreamReader(is));
Vector emergencyVector = (Vector) test.get("root");
emergencyPoliceArray = new ArrayList<Map<String, String>>();
for (int j = 0; j < emergencyVector.size(); j++) {
Hashtable hm = (Hashtable) emergencyVector.get(j);
String title = (String) hm.get("title");
String location = (String) hm.get("location");
Vector phn = (Vector) hm.get("phone");
for (int i = 0; i < phn.size(); i++) {
Hashtable hphn = (Hashtable) phn.get(i);
String phone1 = (String) hphn.get("phn1");
String phone2 = (String) hphn.get("phn2");
String phone3 = (String) hphn.get("phn3");
HashMap<String, String> mp = new HashMap<String, String>();
mp.put("Phn", phone1);
mp.put("Phone2", phone2);
mp.put("Phone3", phone3);
mp.put("NameHeading", "Name");
mp.put("PhnHeading", "Phn no.");
mp.put("LocationHeading", "Location");
mp.put("Title", title);
mp.put("Name", title);
mp.put("Location", location);
emergencyPoliceArray.add(mp);
}
}
} catch (IOException ex) {
}
showForm("EmergencyListDetails", null);
}
最佳答案
如果渲染器条目之间存在差异(例如,一个条目有按钮,而另一个条目没有),这可能会影响所有列表,而不仅仅是按钮可见的条目。
当您进行互动时,我们强烈建议避免列出。对于此类用例来说,它充其量只是黑客行为,并且对于此类常见情况,与容器相比没有任何性能优势。
关于java - 列出 codenameone 中的渲染器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34720225/