标签上的字段更改侦听器

发布于 2024-09-07 00:14:11 字数 950 浏览 3 评论 0原文

我正在从 json url 检索对象列表,并通过添加标签字段和分隔符将其显示为列表。现在,我想让每个标签可点击,以便每个标签重定向到单独的网址。单击标签后,必须打开一个单独的屏幕,其中包含相应 url 的 json 数据。那么,谁能告诉我如何实现这一目标。如果我得到一些如何做到这一点的示例代码,我将非常感激...这是我已经完成的一些示例代码...

public VerticalFieldManager showShoppingList(){
        try {
            jsArrShpList=new JSONArray(strShopping);
            totalList= jsArrShpList.length();
            for(int i=0;i<totalList;i++){
                 String strAlert=jsArrShpList.get(i).toString();
                 JSONObject joAlert=new JSONObject(strAlert);
                 String shoppingList = joAlert.get("CategoryName").toString();
                 LabelField shops  = new LabelField(shoppingList);
                 VerticalFieldManager vfmListRow=new VerticalFieldManager();
                 vfmListRow.add(shops);
                 vfmListRow.add(new SeparatorField());
                 vfmShpList.add(vfmListRow);

            }

            return vfmShpList;

I am retrieving a list of objects from json url and displaying it like a list by adding a label field and a seperator. Now, i want to make each label clickable so that each label redirects to a seperate url. On clicking the label a seperate screen must open with the json data of the corresponding url. So, can anyone tell me how to achieve this. I wil be really grateful if i get some sample code of how to do it... Here's some sample code that i have done...

public VerticalFieldManager showShoppingList(){
        try {
            jsArrShpList=new JSONArray(strShopping);
            totalList= jsArrShpList.length();
            for(int i=0;i<totalList;i++){
                 String strAlert=jsArrShpList.get(i).toString();
                 JSONObject joAlert=new JSONObject(strAlert);
                 String shoppingList = joAlert.get("CategoryName").toString();
                 LabelField shops  = new LabelField(shoppingList);
                 VerticalFieldManager vfmListRow=new VerticalFieldManager();
                 vfmListRow.add(shops);
                 vfmListRow.add(new SeparatorField());
                 vfmShpList.add(vfmListRow);

            }

            return vfmShpList;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叶落知秋 2024-09-14 00:14:11

与其使用一堆通常不接受焦点或单击事件的 LabelField,为什么不使用 ListField 在屏幕上?这似乎更符合您的要求。

如果您确实想采用 LabelField 方法,则需要执行一些操作。首先,当您创建 LabelField 时,请使用 Field.FOCUSABLE 样式,以便它接受焦点:

LabelField shops  = new LabelField(shoppingList, Field.FOCUSABLE);

现在,由于 LabelField 字段不会调用更改监听器(如果您设置了一个),因此您'需要在其父管理器中监听单击和按键事件。由于这些单击或按键事件可以针对管理器中的任何字段,因此您需要在事件发生时检查哪个字段处于焦点状态,并根据焦点字段运行任何适当的处理程序。

代码示例:

VerticalFieldManager vfmListRow = new VerticalFieldManager() {
    protected boolean navigationClick(int status, int time) {
        Field field = getFieldWithFocus();
        if (field != null && field.equals(shops)) {
            System.out.println("shops field clicked");
            return true;
        }
        return super.navigationClick(status, time);
    }

    protected boolean keyChar(char key, int status, int time) {
        Field field = getFieldWithFocus();
        if (key == Characters.ENTER && field != null && field.equals(shops)) {
            System.out.println("shops field clicked");
            return true;
        }
        return super.keyChar(key, status, time);
    }
};

Instead of using a bunch of LabelFields which don't normally accept focus or click events, why not use a ListField on the screen? That seems to be more of what you're looking for.

If you do want to go with the LabelField approach, you'll need to do a few things. First, when you create your LabelField, use the Field.FOCUSABLE style so that it will accept focus:

LabelField shops  = new LabelField(shoppingList, Field.FOCUSABLE);

Now, since the LabelField field won't call into a change listener if you set one up, you'll need to listen for click and key events in it's parent manager. Since these click or key events can be for any field in the manager, you need to check which field is in focus when the event occurs, and run any appropriate handler based on the field in focus.

Code example:

VerticalFieldManager vfmListRow = new VerticalFieldManager() {
    protected boolean navigationClick(int status, int time) {
        Field field = getFieldWithFocus();
        if (field != null && field.equals(shops)) {
            System.out.println("shops field clicked");
            return true;
        }
        return super.navigationClick(status, time);
    }

    protected boolean keyChar(char key, int status, int time) {
        Field field = getFieldWithFocus();
        if (key == Characters.ENTER && field != null && field.equals(shops)) {
            System.out.println("shops field clicked");
            return true;
        }
        return super.keyChar(key, status, time);
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文