在ListCellRenderer中,在LWUIT中显示列表时多次调用了getListCellRendererComponent()方法. [英] In ListCellRenderer getListCellRendererComponent() method called more than once while showing list in LWUIT

查看:92
本文介绍了在ListCellRenderer中,在LWUIT中显示列表时多次调用了getListCellRendererComponent()方法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 j2me 中使用 LWUIT j2me 中运行 S60 .

I'm running S60 with j2me using LWUIT in Eclipse.

我正在使用此教程链接. 以供参考.

I'm using this Tutorial Link.. for reference to draw list.

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

but as i printed the position of list element drawing but it called more than once (i think for 3-4 times) i don't understand why ?

列表的响应性也很差,需要大量时间才能向下滚动.

And also responsiveness of list is also poor, takes lot of time to scroll down .

正如我所见的LWUIT列表的一些视频示例一样,它们的运行速度更快,更流畅,因为我的列表不能解释为什么

As i'e seen some video examples of LWUIT list they are working faster and smoother as my list can not why ??

我要解析的代码并将值从接收到的json添加到列表中

My code to parse and add values to list from json received

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;
    }

}

请帮助我...

推荐答案

在Codename One博客上阅读Chen的帖子

Read Chen's post on the Codename One blog http://codenameone.blogspot.com/2008/07/lwuit-list-renderer-by-chen-fishbein.html

通常,将为每个可见项调用一次单元格渲染器,并且在移动它时,甚至会调用它更多,因为背景中运行着焦点移动的动画. S60处理半透明图像的速度真的很慢,请确保如果您有9个边框,它们不会包含过小的图像.

Generally a cell renderer will be called once for every visible item and when it is moved it will be called even more since there is an animation of the focus movement running in the background. S60 is really slow with translucent images, make sure that if you have 9-piece borders they don't contain images that are too small.

将性能与LWUIT演示进行比较,并观察差异.

Compare performance to the LWUIT demo and see the difference.

这篇关于在ListCellRenderer中,在LWUIT中显示列表时多次调用了getListCellRendererComponent()方法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆