2

我在 Eclipse 中使用LWUIT运行带有j2me的S60 。

我正在使用此教程链接..以供参考绘制列表。

但是当我打印列表元素绘图的位置时,它不止一次调用(我认为是 3-4 次)我不明白为什么?

而且列表的响应能力也很差,向下滚动需要很多时间。

正如我看到的一些 LWUIT 列表的视频示例,它们工作得更快、更流畅,因为我的列表不知道为什么?

我的代码从收到的 json 解析和添加值到列表

public void setNotificationList(String response) {

    if (!response.equals("") || response != null) {

        try {
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject != null) {
                if (jsonObject.getInt("total") > 0) {
                    totalNotificationPages = jsonObject
                            .getInt("total_pages");

                    JSONArray notificationJsonArray = jsonObject
                            .getJSONArray("notifications");
                    int lenghtOfList = notificationJsonArray.length();

                    if (!moreNotificationListFalg) {
                        notificationList = new List();
                    } else {
                        notificationVector.removeAllElements();
                    }

                    notificationVector = new Vector(lenghtOfList);

                    for (int i = 0; i < lenghtOfList; i++) {

                        JSONObject notificationJsonObjects = notificationJsonArray
                                .getJSONObject(i);
                        UpdatesUserData notificationData = new UpdatesUserData();

                        notificationData.user_id = JSONHelper
                                .getStringFromJSON(notificationJsonObjects,
                                        "user_id");
                        notificationData._id = JSONHelper
                                .getStringFromJSON(notificationJsonObjects,
                                        "id");
                        notificationData.message = JSONHelper
                                .getStringFromJSON(notificationJsonObjects,
                                        "message");

                        notificationData.date = JSONHelper
                                .getStringFromJSON(notificationJsonObjects,
                                        "created_at");

                        // Helper.stringToDate(JSONHelper.getStringFromJSON(notificationJsonObjects,
                        // "created_at"));
                        // Helper.createDate(JSONHelper.getStringFromJSON(notificationJsonObjects,
                        // "created_at"));

                        notificationData.distance = JSONHelper
                                .getStringFromParentJSON(
                                        notificationJsonObjects, "user",
                                        "distance");

                        if (notificationJsonObjects.has("user")) {
                            JSONObject jsonObjectParent = notificationJsonObjects
                                    .getJSONObject("user");

                            notificationData.user_image = APIHelper
                                    .getPictureUrl(
                                            JSONHelper
                                                    .getStringFromParentJSON(
                                                            jsonObjectParent,
                                                            "profile",
                                                            "avatar_thumb"),
                                            JSONHelper
                                                    .getStringFromParentJSON(
                                                            jsonObjectParent,
                                                            "profile",
                                                            "picture_url"));

                        }
                        System.out.println("User Image "
                                + notificationData.user_image);
                        notificationVector.addElement(notificationData);

                    }// for loop ends

                    System.out.println("Displaying user Notification list");

                    // check if Update List Exists then remove it and Add
                    // this Notification List

                    if (form.contains(updateList)) {
                        form.removeComponent(updateList);
                        System.out.println(" Update List removed...");
                    }
                    if (form.contains(moreUpdates)) {
                        form.removeComponent(moreUpdates);
                        System.out.println(" moreUpdate removed... ");
                    }

                    // ----------------------------------------------------

                    // Getting index of lat element of List before adding
                    // new elements to it
                    int index = notificationList.size();

                    if (notificationVector.size() > 0) {

                        // Adding elements to notification vector
                        for (int i = 0; i < notificationVector.size(); i++) {
                            notificationList.addItem(notificationVector
                                    .elementAt(i));
                        }

                        System.out.println("Adding List ");

                        if (!moreNotificationListFalg) {
                            notificationList
                                    .setRenderer(new UpdatesListCellRenderer(
                                            false));

                            form.addComponent(notificationList);
                        } else {

                            notificationList.setSelectedIndex(index - 1);
                            notificationList.repaint();
                        }

                        // Addgin more... Btn if pages are more than 1
                        if (totalNotificationPages > notificationPageNo)
                            form.addComponent(moreUpdates);

                        // Adding ActionListener to list
                        notificationList
                                .addActionListener(new ActionListener() {

                                    public void actionPerformed(
                                            ActionEvent arg0) {

                                        form.setFocused(notificationTabBtn);

                                    }
                                });
                    }
                    moreNotificationListFalg = false;// setting flag false
                    form.invalidate();
                    form.repaint();

                } else {
                    System.out.println("List has not Elements to print.");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}

我的渲染器类

public class UpdatesListCellRenderer extends Container implements
        ListCellRenderer {

    private Label message = new Label("");
    private Label distance = new Label("");
    private Label profile_image = new Label("");
    private Label date = new Label("");
    private Label focus = new Label();
    private Font font;
    public Container base_container;
    private Container containerY,containerX;
    private Resources resources;
    private boolean flag = false;
    private Button moreUpdates = new Button();

    public UpdatesListCellRenderer(boolean flag) {

        try {
            setCellRenderer(true);
            this.flag = flag;
            BorderLayout bl = new BorderLayout();
            setLayout(bl);

            base_container = new Container(new BoxLayout(BoxLayout.X_AXIS));

            // cnt1.setWidth(width);

            resources = Resources.open("/font.res");

            font = resources.getFont("font1");

            base_container.getStyle().setBgImage(
                    Image.createImage("/images/contact_list_background.png"));

            profile_image.getStyle().setBgTransparency(0);
            profile_image.setPreferredH(60);
            profile_image.setPreferredW(70);
            profile_image.getStyle().setAlignment(CENTER);
            base_container.addComponent(profile_image);

            containerY = new Container(new BoxLayout(BoxLayout.Y_AXIS));

            // name.getStyle().setBgTransparency(0);
            // name.getStyle().setFont(font);

            message.getStyle().setBgTransparency(0);
            message.getStyle().setFont(font);

            containerX = new Container(new BoxLayout(BoxLayout.X_AXIS));
            containerX.getStyle().setAlignment(BOTTOM);
            containerX.getStyle().setMargin(25, 0, 0, 0);

            distance.setAlignment(LEFT);
            distance.setPreferredH(20);
            distance.setPreferredW(85);
            distance.getStyle().setBgImage(
                    Image.createImage("/images/long_right_arrow.png"));
            distance.getStyle().setBgTransparency(0);
            distance.getStyle().setFont(font);

            date.setAlignment(RIGHT);
            date.getStyle().setBgTransparency(0);
            date.getStyle().setFont(font);
            date.getStyle().setMargin(0, 0, 100, 1);


            //moreUpdates.setVisible(false);

            // containerY.addComponent(name);
            containerY.addComponent(message);
            containerX.addComponent(distance);
            containerX.addComponent(date);

            containerY.addComponent(containerX);

            base_container.addComponent(containerY);
            addComponent(BorderLayout.CENTER, base_container);

            //addComponent(BorderLayout.SOUTH, moreUpdates);



            focus.getStyle().setBgTransparency(100);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public Component getListCellRendererComponent(List list, Object value,
            int position, boolean isSelected) {
        UpdatesUserData notificationData = (UpdatesUserData) value;

        try {

            //System.out.println(" List get ListComponent Popstion " + position + "  List size  " + list.size());

            //if (!notificationData.message.equals(""))
                message.setText("" + notificationData.message);
//          else
//              message.setText("");

            //if (!notificationData.distance.equals(""))
                distance.setText("" + notificationData.distance);
//          else
//              distance.setText("" + notificationData.distance);

            date.setAlignment(RIGHT);
            //if (!notificationData.date.equals(""))
                date.setText("" + notificationData.date.substring(0, 10));
//          else
//              date.setText("");
            date.setAlignment(RIGHT);

            profile_image.setIcon(Helper.loadImage(notificationData.user_image));

//          if( position == list.size()-1){
//              //base_container.addComponent(moreUpdates);
//              //addComponent(BorderLayout.SOUTH, moreUpdates);
//              moreUpdates.setVisible(true);
//          }




        } catch (IOException e) {
            e.printStackTrace();
        }

        if (isSelected) {
            list.setFocus(true);
            list.getStyle().setBgTransparency(100);
        } else {
            list.setFocus(false);
            list.getStyle().setBgTransparency(0);
        }

        return this;
    }

    public Component getListFocusComponent(List list) {

        return focus;
    }

}

请帮我 ...

4

4 回答 4

2

阅读 Chen 在 Codename One 博客上的帖子http://codenameone.blogspot.com/2008/07/lwuit-list-renderer-by-chen-fishbein.html

通常,每个可见项目都会调用一次单元格渲染器,当它移动时,它会被调用更多次,因为在后台运行焦点移动的动画。S60 处理半透明图像真的很慢,如果你有 9 片边框,请确保它们不包含太小的图像。

将性能与 LWUIT 演示进行比较并查看差异。

于 2012-07-15T18:38:03.987 回答
1

我认为这段代码中的问题:profile_image.setIcon(Helper.loadImage(notificationData.user_image));. 尝试在没有此代码的情况下使用您的渲染或将其替换为存根图像,让我们看看会发生什么。

于 2012-07-12T06:01:38.803 回答
0

好的,我想它打印了这么多次来刷新状态List

您是否使用ListModel附加到List? 这一事实对于列表的效率很重要。您如何映射列表的元素?你可以在你的问题中粘贴一些代码吗?

于 2012-07-11T09:13:19.403 回答
0

正如我认为 list 被调用它包含的每个元素来绘制它。我的问题与我实际上从 getListCellRendererComponent() 中的 url 加载图像相同,错误的是我之前实际加载它(在解析 json 时)并存储然后将其作为图像传递给列表。那件事大大提高了我的列表列表性能。

顺便说一句,谢谢大家的帮助。。

于 2012-07-16T07:24:57.097 回答