Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 7
Registered: ‎04-15-2010
My Device: blackberry 9700
My Carrier: tim
Accepted Solution

Listfield and navigationclick

[ Edited ]

Hi,

 i've read this tutorial

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800345/...

 

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

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Listfield and navigationclick

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.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: Listfield and navigationclick

 

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

 

Thanks and Regards,
PraveenGoparaju.
Developer
Posts: 136
Registered: ‎01-25-2010
My Device: Z30, Z10, Q10, Dev Alpha B & Bold 9900
My Carrier: Bell

Re: Listfield and navigationclick

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 !

 

 

New Contributor
Posts: 7
Registered: ‎04-15-2010
My Device: blackberry 9700
My Carrier: tim

Re: Listfield and navigationclick

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?

 

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Listfield and navigationclick

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)

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Contributor
Posts: 7
Registered: ‎04-15-2010
My Device: blackberry 9700
My Carrier: tim

Re: Listfield and navigationclick

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

  }
}

 

 

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Listfield and navigationclick

retrieve elements from a vector using elementAt(index i), not [index]

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Contributor
Posts: 7
Registered: ‎04-15-2010
My Device: blackberry 9700
My Carrier: tim

Re: Listfield and navigationclick

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!

Highlighted
New Contributor
Posts: 7
Registered: ‎04-15-2010
My Device: blackberry 9700
My Carrier: tim

Re: Listfield and navigationclick

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

  }