黑莓机:更改KeywordFilterField的排序 [英] Blackberry: change sorting of a KeywordFilterField

查看:83
本文介绍了黑莓机:更改KeywordFilterField的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我准备了一个简单的测试用例来说明我的问题.

I have prepared a simple test case demonstrating my problem.

它只有1个文件(CheckMenu.java,在下面列出),并且可以立即运行.

It is just 1 file (CheckMenu.java, listed below) and instantly runnable.

一旦用户通过选择2个MenuItem中的1个更改了排序方法,我如何重绘基础

Once the user changes the sorting method by selecting 1 of the 2 MenuItems, how do I redraw the underlying KeywordFilterField? I've tried calling myList.updateList() and myList.invalidate() - it doesn't help - the myList items are not reorganized.

我还想知道,用什么代替已过时的MenuItem.setText(String)?

Also I wonder, what to use instead of the deprecated MenuItem.setText(String)?

package mypackage;

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.util.Comparator;
import net.rim.device.api.util.StringUtilities;
import net.rim.device.api.collection.util.*;

public class CheckMenu extends UiApplication {
    public static void main(String[] args) {
        CheckMenu myApp = new CheckMenu();       
        myApp.enterEventDispatcher();
    }

    public CheckMenu() {        
        pushScreen(new MyScreen());
    }    
}

class MyScreen extends MainScreen {
    KeywordFilterField myList = new KeywordFilterField();
    MyItemList myItems = new MyItemList();

    public MyScreen() {
        setTitle(myList.getKeywordField());

        myItems.doAdd(new MyItem(1, "Eins"));
        myItems.doAdd(new MyItem(2, "Zwei"));
        myItems.doAdd(new MyItem(3, "Drei"));
        myItems.doAdd(new MyItem(4, "Vier"));

        myList.setSourceList(myItems, new MyItem.MyProvider());
        // XXX commenting the line below does not help
        myList.setCallback(new MyListFieldCallback());
        add(myList);
    }

    private MenuItem numMenu = new MenuItem("num sort", 0, 0) {  
        public void run() {
            MyItem.NUMERIC_SORT = true;
            Status.show("Use " + toString());
            setText("num sort \u221A");
            alphaMenu.setText("alpha sort");
            myList.updateList();
        }
    };
    private MenuItem alphaMenu = new MenuItem("alpha sort", 1, 0) {  
        public void run() {
            MyItem.NUMERIC_SORT = false;
            Status.show("Use " + toString());
            setText("alpha sort \u221A");
            numMenu.setText("num sort");
            myList.updateList();
        }
    };

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(numMenu);
        menu.add(alphaMenu);
    }

    private class MyListFieldCallback implements ListFieldCallback {

        public void drawListRow(ListField list, Graphics g, int index, int y, int width) {
            Object obj = ((KeywordFilterField)list).getElementAt(index);
            if(obj != null && obj instanceof MyItem) {
                MyItem item = (MyItem) obj;
                g.drawText(item.toString(), 20 * (1 + index), y);
            } else if(index == 0) {
                g.drawText(list.getEmptyString(), 0, y);
            }
        }

        public Object get(ListField list, int index) { 
            return null; 
        }

        public int getPreferredWidth(ListField list) { 
            return 0; 
        }

        public int indexOfList(ListField list, String prefix, int start) { 
            return 0; 
        }
    }
}

class MyItemList extends SortedReadableList {
    public MyItemList() {
        super(new MyItem.MyComparator());        
    } 

    protected void doAdd(Object obj) {
        super.doAdd(obj);   
    }

    protected boolean doRemove(Object obj) {
        return super.doRemove(obj);        
    }
}

class MyItem {
    public static boolean NUMERIC_SORT;

    private int _num;
    private String _name;

    public MyItem(int num, String name) {
        _num = num;
        _name = name;
    }

    public String toString() {
        return _num + ": " + _name;
    }

    static class MyComparator implements Comparator {
        public int compare(Object obj1, Object obj2) {
            if (! (obj1 instanceof MyItem && obj2 instanceof MyItem))
                throw new IllegalArgumentException("Cannot compare non-MyItems");

            MyItem item1 = (MyItem) obj1;
            MyItem item2 = (MyItem) obj2;

            if (MyItem.NUMERIC_SORT) {
                if (item1._num == item2._num)
                    return 0;
                return (item1._num > item2._num ? 1 : -1);
            }

            return item1._name.compareTo(item2._name);
        }
    }

    static class MyProvider implements KeywordProvider {
        public String[] getKeywords(Object obj) {
            if (obj instanceof MyItem) {
                MyItem item = (MyItem) obj;
                return new String[]{ Integer.toString(item._num), item._name };
            }
            return null;
        }
    }
}

更新:我已经更新了代码,以使用Arhimed建议的makeMenu(). 我还发布了我的问题在BlackBerry论坛上.

UPDATE: I've updated the code to use makeMenu() as suggested by Arhimed. I've also posted my question at the BlackBerry forum.

谢谢! 亚历克斯

推荐答案

也许我不明白您的问题.....但是,正如我所看到的,此代码在模拟器上可以正常工作

Maybe I don't understand your problem..... But as I see, this code works fine on simulator

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.util.Comparator;
import net.rim.device.api.util.StringUtilities;
import net.rim.device.api.collection.util.*;

public class CheckMenu extends UiApplication {
    public static void main(String[] args) {
        CheckMenu myApp = new CheckMenu();       
        myApp.enterEventDispatcher();
    }

    public CheckMenu() {        
        pushScreen(new MyScreen());
    }    
}

class MyScreen extends MainScreen {
    KeywordFilterField myList = new KeywordFilterField();
    MyItemList myItems = new MyItemList();

    public MyScreen() {
        setTitle(myList.getKeywordField());

        myItems.doAdd(new MyItem(1, "Eins"));
        myItems.doAdd(new MyItem(2, "Zwei"));
        myItems.doAdd(new MyItem(3, "Drei"));
        myItems.doAdd(new MyItem(4, "Vier"));

        myList.setSourceList(myItems, new MyItem.MyProvider());
        // XXX commenting the line below does not help
        myList.setCallback(new MyListFieldCallback());
        add(myList);
    }

    private MenuItem numMenu = new MenuItem("num sort", 0, 0) {  
    public void run() {
        MyItem.NUMERIC_SORT = true;
        Status.show("Use " + toString());
        setText("num sort \u221A");
        alphaMenu.setText("alpha sort");
        myItems.sort();
        myList.updateList();
    }
};
private MenuItem alphaMenu = new MenuItem("alpha sort", 1, 0) {  
    public void run() {
        MyItem.NUMERIC_SORT = false;
        Status.show("Use " + toString());
        setText("alpha sort \u221A");
        numMenu.setText("num sort");
        myItems.sort();
        myList.updateList();
    }
};

protected void makeMenu(Menu menu, int instance) {
    super.makeMenu(menu, instance);
    menu.add(numMenu);
    menu.add(alphaMenu);
}

private class MyListFieldCallback implements ListFieldCallback {

    public void drawListRow(ListField listField, Graphics g, int index, int y, int    width) {
        Object obj = ((KeywordFilterField)listField).getElementAt(index);
        if(obj != null && obj instanceof MyItem) {
            MyItem item = (MyItem) obj;
            g.drawText(item.toString(), 20 * (1 + index), y);
        } else if(index == 0) {
            g.drawText("ssssssss", 0, y);
        }
    }

    public Object get(ListField listField, int index) { 
        return null; 
    }

    public int getPreferredWidth(ListField listField) { 
        return 0; 
    }

    public int indexOfList(ListField listField, String prefix, int start) { 
        return 0; 
    }
}
}

class MyItemList extends SortedReadableList {
    public MyItemList() {
        super(new MyItem.MyComparator());        
    } 

    protected void doAdd(Object obj) {
        super.doAdd(obj);   
    }

    protected boolean doRemove(Object obj) {
        return super.doRemove(obj);        
    }
 }

class MyItem {
    public static boolean NUMERIC_SORT;

    private int _num;
    private String _name;

    public MyItem(int num, String name) {
        _num = num;
        _name = name;
    }

    public String toString() {
        return _num + ": " + _name;
    }

    static class MyComparator implements Comparator {
        public int compare(Object obj1, Object obj2) {
            if (! (obj1 instanceof MyItem && obj2 instanceof MyItem))
                throw new IllegalArgumentException("Cannot compare non-MyItems");

            MyItem item1 = (MyItem) obj1;
            MyItem item2 = (MyItem) obj2;

            if (MyItem.NUMERIC_SORT) {
                if (item1._num == item2._num)
                    return 0;
                return (item1._num > item2._num ? 1 : -1);
            }

            return item1._name.compareTo(item2._name);
        }
    }

    static class MyProvider implements KeywordProvider {
        public String[] getKeywords(Object obj) {
            if (obj instanceof MyItem) {
                MyItem item = (MyItem) obj;
                return new String[]{ Integer.toString(item._num), item._name };
            }
        return null;
    }
}
}

这篇关于黑莓机:更改KeywordFilterField的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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