I used list renderer and all works fine. Then I kept a extract last clicked item as follows: There are 3 buttons in the renderer. button1 works in all, however button2 and button3 works in some and doesn't work in other? why is it?
If I remove if (lastClickedButton != null)
from the code below, it gives NullPointerException
for the same button which works in other list items before.
MyCode:
@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;
}
MyConnection:
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);
}
If there is variance between the renderer entries (e.g. one entry has the button while another doesn't) this might impact all the list and not just the entry where the button is visible.
When you have interaction we strongly recommend avoiding list. Its hackish at best for such use cases and doesn't provide any performance advantages over container for such common cases.