时间:2011-09-29 08:56:07

标签: java java-me lwuit

我正在与LWUIT List合作。以下是List的创建方式:

db = new ClientDB();
listclient = db.listClient();
tModel = new ListSource(listclient);
renderer = new CListCell(true, new String[]{"", "", "Encours : ", "Impayés : "});
tList = new CList(tModel, renderer);

public Vector listClient()
    {
        try
        {
            filter = new Filter("");
            comparator = new Comparator();
            enumRec = this.enumerateRecords(filter, comparator);
            while(enumRec.hasNextElement())
            {
                id = enumRec.nextRecordId();
                recIDs.addElement(new Integer(id));
                bData = this.getRecord(id);
                client = new Client(bData);
                if (!innerResultat.isEmpty())
                {
                    innerResultat.removeAllElements();
                }
                innerResultat.addElement(new String(client.getIdclient()));
                innerResultat.addElement(new String(client.getCin()));
                innerResultat.addElement(new String(client.getNoms()));
                innerResultat.addElement(new String(client.getEncours()));
                innerResultat.addElement(new String(client.getImpayes()));
                resultat.addElement(innerResultat);
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return resultat;
    }
public class ListSource extends DefaultListModel {

    public ListSource(Vector valeur)
    {
        super(valeur);
    }
    public int getListIDs(int ligneSelected)
    {
        return ligneSelected;
    }
}
public class CListCell extends Container implements ListCellRenderer {

    private boolean isPhotoDisplayed;

    private BorderLayout photoLayout = new BorderLayout();
    private BoxLayout yLayout = new BoxLayout(BoxLayout.Y_AXIS);

    private Vector vLabels = new Vector();
    private Label pic = new Label("");
    private Container cnt;

    private Font fontLibelle = (MenuPrincipalForm.r).getFont("FontTextFieldBold");

    private Label focus = new Label("");

    public CListCell(boolean displayPhoto, String[] libelles)
    {
        isPhotoDisplayed = displayPhoto;
        this.getSelectedStyle().setPadding(0, 0, 0, 0);
        this.getUnselectedStyle().setPadding(0, 0, 0, 0);
        if (displayPhoto)
        {
            setLayout(photoLayout);
            addComponent(BorderLayout.WEST, pic);
        }
        else
        {
            setLayout(yLayout);
        }
        cnt = new Container(yLayout);
        for (int i=0;i<libelles.length;i++)
        {
            vLabels.addElement(new Label(libelles[i]));
            ((Label)vLabels.elementAt(i)).getStyle().setBgTransparency(0);
            ((Label)vLabels.elementAt(i)).getStyle().setFont(fontLibelle);
            cnt.addComponent(((Label)vLabels.elementAt(i)));
        }
        addComponent(BorderLayout.CENTER, cnt);
        focus.getStyle().setBgTransparency(100);
    }
    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
    {
        Vector reelData = new Vector();
        if (value != null && (value instanceof Vector))
        {
            if (((Vector)value).isEmpty())
            {
                if (isPhotoDisplayed)
                    pic.setIcon(null);
                for (int i=0; i<vLabels.size(); i++)
                    ((Label)vLabels.elementAt(i)).setText(((Label)vLabels.elementAt(i)).getText().concat("-"));
            }
            else
            {
                reelData = (Vector)((Vector)value).elementAt(index);
                for (int i=0; i<vLabels.size(); i++)
                    ((Label)vLabels.elementAt(i)).setText(((Label)vLabels.elementAt(i)).getText().concat(String.valueOf(reelData.elementAt(i+1))));
                pic.setIcon(null);
            }
        }
        return this;
    }
    public Component getListFocusComponent(List list)
    {
        return focus;
    }
}
public class CList extends List {
    public CList(ListSource model, CListCell renderer)
    {
        setModel(model);
        setListCellRenderer(renderer);
        setItemGap(0);
        setFixedSelection(List.FIXED_NONE_CYCLIC);
    }
}

getListCellRendererComponent的{​​{1}}方法中,ListCellRenderer参数会导致问题。它的类型在Javadoc中是value,在我的应用程序中它的真实数据类型是Object,它是Vector of Vector(Vector的元素是Vector)。

我的问题是我在运行时遇到Vector错误,堆栈错误显示ClassCastException方法导致错误。

那么如何使用Object和Vector?

堆栈错误是:

getListCellRendererComponent

1 个答案:

答案 0 :(得分:0)

一般来说,检查演员是否可行是一个好主意。在你的情况下,你似乎得到一些不是Vector的对象。

if (value != null && value instanceof Vector) {
    Vector vector = (Vector) value;

    ... your body with vector instead of value
} else {
    throw new IllegalArgumentException("Man I didn't get a Vector!");
}

你考虑过使用泛型吗?