在黑莓中选择菜单时重复列表字段元素?

时间:2011-11-24 05:14:27

标签: blackberry

我正在创建一个黑莓应用程序。在这里我使用自定义列表。当我选择菜单项选择时,第一次它工作正常,我再次选择菜单再次添加列表元素。

我想通过再次点击来显示一个时间列表元素而不是重复元素。

如何删除列表的先前元素。

我的代码在这里

    public class SecondScreen extends MainScreen  {

/*private LabelField tab1;
private LabelField tab2;
private LabelField tab3;*/

private CustomButton tab1;
private CustomButton tab2;
private CustomButton tab3;
private LabelField spacer1;
private LabelField spacer2;
private VerticalFieldManager tabArea;
private LabelField tab1Heading;
private BasicEditField tab1Field1;
private BasicEditField tab1Field2;
private LabelField tab2Heading;
private BasicEditField tab2Field1;
private BasicEditField tab2Field2;
private LabelField tab3Heading;
private BasicEditField tab3Field1;
private BasicEditField tab3Field2;
private VerticalFieldManager tab1Manager;
private VerticalFieldManager tab2Manager;
private VerticalFieldManager tab3Manager;
private BitmapField bmpfield;
private LabelField title;
static Font fontTitleTop;
//XmlParse parse;
ListField myList;



public SecondScreen() {



    fontTitleTop = Font.getDefault().derive(Font.BOLD, 25);

    final Bitmap logo = Bitmap.getBitmapResource("title_bg.png");
    HorizontalFieldManager hM = new HorizontalFieldManager(Field.FIELD_TOP|Field.FIELD_HCENTER)
    {
        public void paintBackground(Graphics g)
        {
            g.drawBitmap(0,0,Display.getHorizontalResolution(),50,logo,0,0);
            g.setColor(Color.WHITE);
            g.setFont(SecondScreen.fontTitleTop);
            if(Display.getWidth()>=360){
                g.drawText("Category",((Display.getWidth()/14)),10,0);
            }
            super.paintBackground(g);
        }            

        protected void sublayout(int maxWidth, int maxHeight) 
        {

            setExtent(maxWidth, 50);

        }

    };

    //hM.add(title);
    add(hM);



}

protected void makeMenu(Menu menu, int instance)
{
    super.makeMenu(menu, instance);
    menu.add(new MenuItem("Rss",10,10) {

        public void run() {

            String url = "http://192.168.0.118/rss_new/categories.xml";
            XmlParse.category.removeAllElements();

            XmlParse xp = new XmlParse();
            xp.getDetails(url);

           myList = new MyListField(XmlParse.category);
            myList.setSize(XmlParse.category.size());
            System.out.println("Xml Parsing");
            add(myList);

        }
    });

    menu.add(new MenuItem("Fav",10,10) {

        public void run() {

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

        }
    });
}




private class MyListField extends ListField implements ListFieldCallback
{


    private Vector content = new Vector();
    private Vector content2 = new Vector();
    MyListField  list ;


    public MyListField(Vector content) {

        this.content = content;
        }

    public void drawListRow(ListField listField, Graphics graphics,
            int index, int y, int width) {

        list = (MyListField)listField;

        String title = content.elementAt(index).toString();
        System.out.println("Content "+title+" at index"+ index);

        TableManager tm = new TableManager(title, index);
        tm.drawRow(graphics, 0, y, width, listField.getRowHeight());


    }

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

protected  boolean navigationClick(int status, int time) 
    {

          int selectedIndex = list.getSelectedIndex();
        System.out.print("List selected index ::"+selectedIndex);
        ThirdScreen thirdScreen = new ThirdScreen(selectedIndex);
        UiApplication.getUiApplication().pushScreen(thirdScreen);



        return true;
    }


}

2 个答案:

答案 0 :(得分:1)

您正在MenuItem中创建makeMenu(),每当屏幕需要显示菜单时,系统会调用addMenuItem(MenuItem)。这意味着每次用户按下菜单按钮时,您都会创建新的菜单项,这些菜单项将添加到先前创建的菜单项中,因此会显示重复项。

将MenuItem创建代码移动到屏幕的构造函数中。由于构造函数只被调用一次,因此您不必担心正在创建重复的MenuItem。

要在构造函数中添加菜单项,只需使用makeMenu

请注意,现在无需覆盖{{1}},因此您可以将其删除。

答案 1 :(得分:0)

makeMenu中的第一件事(或者第二件事,如果你想打电话给super.makeMenu)应该是这句话:

menu.deleteAll();

否则,每次按下菜单按钮,除了已经存在的条目外,还会添加新条目。