When you create a row, you are probably creating a (subclass of) Manager
for each row.
At least, it seems like you are creating a ButtonField
on each row.
What you can do is to attach a cookie to each row, or to each button, when you create it. A cookie is just an extra piece of information that's attached to an object. Then, when that row or button is clicked, you ask the row/button for the cookie, and use that to identify the row ID.
Every BlackBerry Field can have a cookie attached to it. Since the cookie is of type Object
, you can make it anything you want.
For example, when creating the buttons for your rows:
for (int i = 0; i < numRows; i++) {
BitmapButtonField button = new BitmapButtonField(onImage, offImage, ButtonField.CONSUME_CLICK);
// use the row index as the cookie
button.setCookie(new Integer(i));
button.setChangeListener(this);
Manager row = new MyRowManager();
row.add(button);
add(row);
}
and then when the button is clicked:
void fieldChanged(Field field, int context) {
Object cookie = field.getCookie();
if (cookie instanceof Integer) {
Integer rowId = (Integer)cookie;
System.out.println("Row Id = " + rowId);
}
}
Note: I'm using the BlackBerry Advanced UI BitmapButtonField for this, but the cookie technique will work with any Field
, or Manager
class. See another example here.