codenameone 中的列表渲染器问题

List renderer issue in codenameone

我使用了列表渲染器,一切正常。然后我保留了一个提取最后点击的项目如下:渲染器中有 3 个按钮。 button1 在所有情况下都有效,但是 button2 和 button3 在某些情况下有效而在其他情况下无效?为什么? 如果我从下面的代码中删除 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);
    }

如果渲染器条目之间存在差异(例如,一个条目有按钮而另一个没有)这可能会影响所有列表,而不仅仅是按钮可见的条目。

当您进行互动时,我们强烈建议避免使用列表。对于此类用例,它充其量只是骇人听闻,并且对于此类常见情况,与容器相比没有任何性能优势。