Java me 如何制作一个LWUIT列表,以根据每个项目内容呈现其项目?

Java me 如何制作一个LWUIT列表,以根据每个项目内容呈现其项目?,java-me,lwuit,nokia,lwuit-list,Java Me,Lwuit,Nokia,Lwuit List,我有一个LWUIT列表,其中包含代表航班对象的项目,每个航班项目显示其航班号,每个航班的出发和到达时间以及机票价格 有些航班有两个航段,航段是指涉及中途停留、更换飞机或更换航空公司的航班段,因此这些航班项目需要与每个航段的起飞时间和到达时间以及每个航段上的始发地和目的地代码一起呈现 起初,只有一条腿的项目正常呈现,但当我滚动列表并开始呈现多条腿的项目时,这些项目被错误地呈现,信息被剪切,更糟糕的是,列表上的所有项目都受此影响,即使是只有一条腿的项目 在此屏幕截图中,您可以看到列表项目在到达具有多

我有一个LWUIT列表,其中包含代表航班对象的项目,每个航班项目显示其航班号,每个航班的出发和到达时间以及机票价格

有些航班有两个航段,航段是指涉及中途停留、更换飞机或更换航空公司的航班段,因此这些航班项目需要与每个航段的起飞时间和到达时间以及每个航段上的始发地和目的地代码一起呈现

起初,只有一条腿的项目正常呈现,但当我滚动列表并开始呈现多条腿的项目时,这些项目被错误地呈现,信息被剪切,更糟糕的是,列表上的所有项目都受此影响,即使是只有一条腿的项目

在此屏幕截图中,您可以看到列表项目在到达具有多个支腿的项目之前的外观:

然后,当我滚动列表并到达具有多个支腿的项目时,所有项目都会被剪切支腿时间信息,并将具有多个支腿的第一个项目的时间信息粘贴在其信息的顶部:

下面我提供了我的列表渲染器类:

package com.yallaya.screens.elements;

import com.dotrez.model.Flight;
import com.dotrez.model.Segment;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Font;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.ListCellRenderer;
import com.yallaya.Main;

/*
 * Renderer for a List of Flights to display
 */
public class FlightItem extends Container implements ListCellRenderer {

    /*
     * Components for this List Item
     */
    private Label flightNumberLbl = new Label("");
    private Label origin1Lbl = new Label("");
    private Label destination1Lbl = new Label("");
    private Label origin2Lbl = new Label("");
    private Label destination2Lbl = new Label("");
    private Label priceLbl = new Label("");

    /*
     * Constructor for this List Item
     */
    public FlightItem() {

        this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        this.getStyle().setBgColor(0xf0f0f0);
        this.getStyle().setBgTransparency(255);
        this.getStyle().setMargin(0, 0, 0, 0);
        this.getStyle().setPadding(5, 5, 0, 0);

        flightNumberLbl.getStyle().setMargin(14, 0, 0, 0);

        origin1Lbl.getStyle().setPadding(0, 0, 24, 0);
        origin1Lbl.getStyle().setMargin(0, 0, 0, 0);

        destination1Lbl.getStyle().setPadding(0, 0, 24, 0);
        destination1Lbl.getStyle().setMargin(0, 0, 0, 0);

        origin2Lbl.getStyle().setPadding(0, 0, 24, 0);
        origin2Lbl.getStyle().setMargin(0, 0, 0, 0);

        destination2Lbl.getStyle().setPadding(0, 0, 24, 0);
        destination2Lbl.getStyle().setMargin(0, 0, 0, 0);

        priceLbl.getStyle().setPadding(0, 0, 24, 0);
        priceLbl.getStyle().setMargin(14, 0, 0, 0);
        priceLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));

        this.addComponent(flightNumberLbl);
        this.addComponent(origin1Lbl);
        this.addComponent(destination1Lbl);
        this.addComponent(origin2Lbl);
        this.addComponent(destination2Lbl);
        this.addComponent(priceLbl);

    }

    public void updateColorItem(boolean isSelected){
        if(isSelected){
            this.getStyle().setBgColor(0x9a3d96);
            flightNumberLbl.getStyle().setFgColor(0xffffff);
            origin1Lbl.getStyle().setFgColor(0xffffff);
            destination1Lbl.getStyle().setFgColor(0xffffff);
            origin2Lbl.getStyle().setFgColor(0xffffff);
            destination2Lbl.getStyle().setFgColor(0xffffff);
            priceLbl.getStyle().setFgColor(0xffffff);
        }
        else{
            this.getStyle().setBgColor(0xffffff);
            flightNumberLbl.getStyle().setFgColor(0x9a3d96);
            origin1Lbl.getStyle().setFgColor(0x555555);
            destination1Lbl.getStyle().setFgColor(0x555555);
            origin2Lbl.getStyle().setFgColor(0x555555);
            destination2Lbl.getStyle().setFgColor(0x555555);
            priceLbl.getStyle().setFgColor(0x555555);
        }
    }

    /*
     * Functions called to render this List Item
     */
    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {

        Flight tmp = (Flight) value;
        int displayIndex = index + 1;
        flightNumberLbl.setText(displayIndex +  ". " + Main.localize("VUELO") + " #" + tmp.getFlightNumber());
        Segment tmpSeg = (Segment) tmp.getSegments().elementAt(0);

        String originStr = Main.localize("SALIDA") + " " + "(" + tmpSeg.getOrigin().getCode() + ")" + ": " + tmpSeg.getDepartureTimeString();
        String destinationStr = Main.localize("LLEGADA") + "(" + tmpSeg.getDestination().getCode() + ")" + ": " + tmpSeg.getArrivalTimeString() + "";

        origin1Lbl.setText(originStr);
        destination1Lbl.setText(destinationStr);

        if(tmp.getSegments().size() > 1){

            tmpSeg = (Segment) tmp.getSegments().elementAt(1);

            originStr = Main.localize("SALIDA") + " " + "(" + tmpSeg.getOrigin().getCode() + ")" + ": " + tmpSeg.getDepartureTimeString();
            destinationStr = Main.localize("LLEGADA") + "(" + tmpSeg.getDestination().getCode() + ")" + ": " + tmpSeg.getArrivalTimeString() + "";

            origin2Lbl.setText(originStr);
            destination2Lbl.setText(destinationStr);
        }
        /************************************************************************/
        /******************* UPDATE PIECE OF CODE STARTS HERE *******************/
        /************************************************************************/
        else{
            origin2Lbl.setText("");
            destination2Lbl.setText("");
        }
        /************************************************************************/
        /******************* UPDATE PIECE OF CODE ENDS HERE *******************/
        /************************************************************************/

        priceLbl.setText("$" + tmp.getAdultFare() + " " + tmp.getCurrency().getCode());

        updateColorItem(isSelected);

        return this;
    }

    /*
     * Function called to get this component when it get focus
     */
    public Component getListFocusComponent(List list) {
        return this;
    }

}
在其他平台上,我认为这个问题被称为重影,但我还没有在诺基亚Asha的LWUIT中找到避免这个问题的方法

我知道,由于使用相同的渲染器渲染所有项目,因此这可能是问题的原因,在这种情况下,如果需要,我需要一种方法以不同方式渲染一个项目

当其中一个项目必须显示不同长度的信息时,如何避免列表中的每个项目都被更改

编辑

我更新了我的代码,现在如果当前飞行对象没有多个航段,那么我用空字符串设置第二航段的标签,这就解决了只有一个航段的项目的问题

因此,我将问题的主题改为保留在具有多个航段的航班项目上的问题,这些项目没有正确调整大小,也没有找到调整每个项目以适应其内容的方法,所有项目都绘制为相同的大小

如何确保物品高度调整正确

编辑2

我尝试在if子句中更改渲染器的高度,在该子句中,我使用this.setHeight800;检查模型中有多少腿被称为线段:

但是用这种方法根本无法得到不同的高度和尺寸

然后我试着用这个;终于改变了尺寸:

    if(tmp.getSegments().size() > 1){
        ......
        flightHolder.setPreferredH(800);
        this.setPreferredH(800);
    }
    else{
        ...........
        flightHolder.setPreferredH(200);
        this.setPreferredH(200);
    }
但是所有的项目都是用800渲染的,我想这是因为上一次航班有多条腿,但我真的不知道,我希望为多条腿的航班设置一个更大的高度,然后为只有一条腿的航班重置为一个更小的高度会起作用,但似乎每个项目的高度都是相同的,有人能给我确认一下吗?

好的。试试这个:

首先,您需要声明一个全局变量来保存默认的行高度。然后在构造函数中,将do.setPreferredHeights设置为默认高度全局变量

现在在您的:

if(tmp.getSegments().size() > 1){
将全局变量行高设置为*乘以分段数,您应该具有正确的行高

您可以对边距/填充进行类似的操作,使用段数来相应地定位元素

我希望这有帮助,让我知道你进展如何


Fenix

我之前曾尝试将表示列表项的容器的高度更改为500(如果它有支腿),如果它没有支腿,则更改为300(但列表中的所有项都绘制为相同的高度),或者是否有特定的方法覆盖默认高度?以及如何获得默认行高度?有。您需要使用container.setPreferredHeightyour值。请注意,这可能也会受到容器使用的任何布局管理器的影响。至于默认行高度,您应该自己设置,但如果您是指LWUIT绘制这些行的高度,我认为它是40。
if(tmp.getSegments().size() > 1){