0

I am working on BB OS v5.0. I have managed to get the list to appear on the screen. I am getting data from webservice and adding it into a Vector.

Now I want to find out onclick, which is the item that is clicked and accordingly perform some operation. For that i am trying to display an alert. But I'm not getting the alert.

Here is my code :

In my mainscreen , i added fieldmanager=new VerticalFieldManager(); and add(fieldmanager);

void fetchAlbumsForLetter(String letter) {
    Status.show("Processing ....", 3000);
    fieldManager.deleteAll();

    VerticalFieldManager top = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR | Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR | Field.USE_ALL_WIDTH){
        public void paint(Graphics graphics) {
            graphics.setBackgroundColor(0x00290008);
            graphics.setColor(Color.WHITE);
            graphics.clear();
            graphics.drawBitmap(0, 0, sha.getWidth(),
            sha.getHeight(), sha, 0, 0);
            super.paint(graphics);
        }
    };
    add(top);

    CustomListField4 list4 = new CustomListField4(null){
        protected boolean navigationClick(int status, int time) {
            getValue4();
            return true;
        }
    };
    fieldmanager.add(list4);
}

protected void getValue4() {
    Field f = getFieldWithFocus();
    if (f instanceof ListField) {
        ListField l = (ListField) f;
        final int index = l.getSelectedIndex();
        HistoryItem _contactslist = (HistoryItem) CustomListField4.val4.elementAt(index);
        final String id = _contactslist.getName();
        Dialog.alert(id+"");
    }
}

Please help me to resolve this

EDIT

class CustomListField4 extends ListField implements ListFieldCallback {

    public CustomListField4(Vector data) {
        super(0, ListField.MULTI_SELECT);
        final TableRowManager row = new TableRowManager() {
            public void paint(Graphics g) {
                // g.fillRect(0, 0, getWidth(), getHeight());
                g.setColor(0x0f3e19b);
                g.clear();
                super.paint(g);
            }
        };

        Bitmap icon = Bitmap.getBitmapResource("Devil Skype.png");
        HorizontalFieldManager h=new HorizontalFieldManager();
        h.add(new BitmapField(icon));
        //h.add(new BitmapField(song.getThumb()));
        h.add(new LabelField(song.getAlbumName()));
        //h.add(new LabelField(row1.getLanguage()));
        //h.setMargin(0,0,50,0);
        //Dialog.alert(song.getName());

        VerticalFieldManager vfm=new VerticalFieldManager();
        vfm.add(h);
        //vfm.add(new LabelField(song.getArtist()));
        row.add(vfm);
        contacts.addElement(row);
    }

    setSize( contacts.size());
}

// ListFieldCallback Implementation
public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
    listField.setRowHeight(index,107);
    CustomListField4 list = (CustomListField4) listField;
    TableRowManager rowManager = (TableRowManager) CustomListField4.contacts.elementAt(index);
    rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}

public class TableRowManager extends Manager {
    public TableRowManager() {
        super(0);
    }   
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
meera
  • 199
  • 3
  • 15
  • 1
    Please post some of the code from your `CustomListField4` class. We don't know what that is. I don't see anything above that's wrong, unless it's something in `CustomListField4`, or perhaps in the way you lookup `index` in your contacts list. – Nate May 06 '13 at 09:58
  • @Signare, I didn't understand your comment 100%. You think `fetchAlbumsForLetter()` is being called as a result of a button click? – Nate May 06 '13 at 10:09
  • @user2291839, what does your `CustomListField4` inherit from? What class does it `extend`? – Nate May 06 '13 at 10:14
  • 1
    @Nate class CustomListField4 extends ListField implements ListFieldCallback – Rince Thomas May 06 '13 at 10:20
  • 1
    @Nate you know any solutions ? – Rince Thomas May 06 '13 at 10:24
  • @Nate check the comment on your answer. – Rince Thomas May 06 '13 at 13:04

3 Answers3

1

You are calling getFieldWithFocus() which will give you the manager. You need to get the leaf field

protected void getValue4() {
   Field f = getLeafFieldWithFocus();
   if (f instanceof ListField) {
       //Your code
   }
}
Adwiv
  • 1,283
  • 9
  • 15
  • iam adding the list to the Vertical field manager inside a button click. So when i debug, iam getting Field f = getLeafFieldWithFocus();. But iam not getting f instanceof ListField. f not an instance of listfield. Thats the error iam getting. – Rince Thomas May 07 '13 at 10:11
  • @Signare I hope you understand the difference between `getFieldWithFocus()` and `getLeafFieldWithFocus()`. The first function will give you the field that is direct child of your screen that "contains" focus - in your case the vertical field manager. Using the second function will give you the field object that actually has the focus. – Adwiv May 07 '13 at 10:36
  • What is the type of field that you get? try doing a sysout on the field's class name - `f.getClass.getName()` – Adwiv May 07 '13 at 12:48
  • 1
    Also make sure you are accessing the `getLeafFieldWithFocus()` of the screen and not some other manager. This is possible if your `getValue4()` method is defined inside some manager. You can try doing `getScreen().getLeafFieldWithFocus()` if doing within a manager. – Adwiv May 07 '13 at 12:58
  • What do you mean by f.getClass().getName() is not valid? – Adwiv May 07 '13 at 13:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29573/discussion-between-adwiv-and-meera) – Adwiv May 08 '13 at 05:24
0

I think your hierarchy of Field and Manager objects is incorrect, and this is causing problems with your detection of field focus/selection.

It wasn't obvious from the original code you posted, but by looking at your update, I assume that you are calling fetchAlbumsForLetter() once for every row. That's not right.

fetchAlbumsForLetter() is creating a new CustomListField4 each time it's called. And, CustomListField4 is a ListField.

A ListField is not meant to represent only one row. It's meant to represent all the rows. You should only create one instance of CustomListField4.

I would do either one of two things:

1. Continue to Use a ListField

If you want CustomListField4 to be a ListField (extends ListField), then in your implementation of

public void drawListRow(ListField listField, Graphics g, int index, int y, int width);

you should actually draw graphics objects, using all the Graphics#draw methods. These are primitive graphics items, like filled areas, lines, text, or bitmaps. You would not be using Field objects inside each ListField row, as you're trying to do with your TableRowManager class.

See here for a sample ListField, or here for a more sophisticated example

2. Imitate ListField with a Manager

change your code to

public class CustomListField4 extends VerticalFieldManager {

or

public class CustomListField4 extends Manager {

Then, you can use a TableRowManager for each row, and add LabelField or BitmapField objects to it.

See here for an example of this


If you fix these problems, then I think the way you are overriding navigationClick() will work fine for detecting the row click, and doing something with the selected row.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • when i add the same listfield5 in my mainscreen directly, then no problem occures. The problem occures when i add the list into a field manager. – Rince Thomas May 06 '13 at 13:00
  • also if (f instanceof ListField) is not true in this case, because i addding list to field manager. – Rince Thomas May 06 '13 at 13:01
  • @Signare, there is no `listfield5` in this question or answer, so I'm not sure what you're referring to. My suggestion was to completely re-write this code, because it incorrectly uses field and manager objects. If you try this, and still have problems, please post a new question with the new code you're using. – Nate May 06 '13 at 23:37
  • @Signare, also, you're right that if you change `CustomListField4` to **not** inherit from `ListField`, then the check `f instanceof ListField` will always be false. However, I'm not sure why that check exists anyway. I don't think you need `if (f instanceof` at all. Unless `getValue4()` is called from another `navigationClick()` method, too, it will always be called because of a click on the `CustomListField4`, right? So, I don't think they need that check. – Nate May 06 '13 at 23:57
-1

You can try this

 UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    Dialog.alert(id+"");
                }
            });
mobileDeveloper
  • 894
  • 2
  • 14
  • 35