04-16-2010 06:42 AM - edited 04-16-2010 06:43 AM
Hi,
i've read this tutorial
to create a list of elements on mail screen.
Now i would like, when i select one item of the list, open a new screen for visualize another information.
How can i make it?
In the development guide there is only written to use navigationclick but i don't understand where to use it
Thanks
Solved! Go to Solution.
04-16-2010 07:04 AM
you can use getSelectedIndex to get the index, use it to retrieve the object from the vector/array used with the listfield.
you can overwrite navigationclick and makemenu to trigger your method.
04-16-2010 07:16 AM
protected boolean navigationClick(int status, int time)
{
Field field = this.getFieldWithFocus();
if(field instanceof ListField)
{
// listValues is String[] where you store your list elements.
// listField is the ListField instance you are using
String selectedText = listValues[listField.getSelectedIndex()];
return true;
}
return super.navigationClick(status, time);
}
you can place this code in your custom list field or in the manager class wheere you are adding your list field
04-16-2010 08:18 AM
I've added the following method to the TableFieldList which allows me to retreive the selected row...
public Manager getSelectedRow()
{
int selectedIndex = this.getSelectedIndex();
return _rows[selectedIndex];
}
So, when I instantiate the TableFieldList, I override the navigationClick event with the following code...
public boolean navigationClick(int status, int time)
{
Manager m = this.getSelectedRow();
LabelField selectedIncidentNumber = (LabelField)m.getField(0);
Dialog.alert(selectedIncidentNumber.getText()) ;
return true;
}
It works great !
04-16-2010 09:05 AM
Sorry but i'm just starting to write for bb.
I've put the code
_listElements = new Vector();
_listField = new ListField();
protected boolean navigationClick(int status, int time)
{
Field field = this.getFieldWithFocus();
if(field instanceof ListField)
{
// listValues is String[] where you store your list elements.
// listField is the ListField instance you are using
String selectedText =_listElements[_listField.getSelectedIndex()];
return true;
}
return super.navigationClick(status, time);
}
in my demoScreen class but on this line:
String selectedText =_listElements[_listField.getSelectedIndex()];
i've the error:
The type of the expression must be an array type but it resolved to Vector.
Where i'm wrong?
04-16-2010 09:15 AM
did you implement ListFieldCallback? you have to register it with the listfield, it manages the objects.
there are numerous samples floating around that can sure help you (forums and kb)
04-16-2010 09:31 AM
Yes, this is my entire code:
public class demo extends UiApplication {
public static void main(String[] args) {
demo App = new demo();
App.enterEventDispatcher();
}
public demo() {
pushScreen(new demoScreen());
}
private class demoScreen extends MainScreen {
ListField _listField;
Vector _listElements;
ListCallback _callback;
public demoScreen() {
super();
LabelField title = new LabelField("Demo",LabelField.FIELD_HCENTER | LabelField.USE_ALL_WIDTH);
setTitle(title);
_listElements = new Vector();
_listField = new ListField();
_callback = new ListCallback();
_listField.setCallback(_callback);
add(_listField);
initializeList();
}
private void initializeList()
{
String itemOne = "Element 1";
String itemTwo = "Element 2";
_listElements.addElement(itemOne);
_listElements.addElement(itemTwo);
reloadList();
}
private void reloadList()
{
_listField.setSize(_listElements.size());
}
private class ListCallback implements ListFieldCallback
{
public void drawListRow(ListField list, Graphics g, int index, int y, int w)
{
String text = (String)_listElements.elementAt(index);
g.drawText(text, 0, y, 0, w);
}
public Object get(ListField list, int index)
{
return _listElements.elementAt(index);
}
public int indexOfList(ListField list, String prefix, int string)
{
return _listElements.indexOf(prefix, string);
}
public int getPreferredWidth(ListField list)
{
return Display.getWidth();
}
}
protected boolean navigationClick(int status, int time)
{
Field field = this.getFieldWithFocus();
if(field instanceof ListField)
{
// listValues is String[] where you store your list elements.
// listField is the ListField instance you are using
String selectedText = _listElements[_listField.getSelectedIndex()];
return true;
}
return super.navigationClick(status, time);
}
}
}
04-16-2010 09:38 AM
retrieve elements from a vector using elementAt(index i), not [index]
04-16-2010 10:15 AM
If i understand now i must write the following code:
String selectedText = (String)_listElements.elementAt(_listField.getSelectedIndex());
now, how can i create a new screen from this?
Sorry but i can't find a post that talking about this stuff!
04-20-2010 03:11 AM
I tried this solution and it works:
protected boolean navigationClick(int status, int time)
{
Field field = this.getFieldWithFocus();
if(field instanceof ListField)
{
int selected = _listField.getSelectedIndex();
switch (selected) {
case 0:
pushScreen(new Screen1());
break;
case 1:
pushScreen(new Screen2());
break;
case 2:
pushScreen(new Screen3());
break;
case 3:
pushScreen(new Screen4());
break;
case 4:
pushScreen(new Screen5());
break;
default:
break;
}
return true;
}
return super.navigationClick(status, time);
}
}