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
Taxos
Posts: 9
Registered: ‎06-08-2011
My Device: Q10
Accepted Solution

ListField action listener

Hi all,

 

Im trying to create a list with a ListField thats contains a Vector list.

 

Now I want to put an action listener to the to the ListField so when someone cliks something on the list the application catches that.

 

I've tried to do this by adding listfield.setChangeListener but this doesnt work :smileysad:

 

Heres my code.

 

public class ServerSelect extends MainScreen {
	private Main main;
	private ServerList serverlist;
	private Bitmap online, offline;
	private Vector list;
	private ListField listfield;
	private ListCallback callback;
	
	public ServerSelect(Main main)
	{
		this.main = main;
		online = Bitmap.getBitmapResource("talking.jpg");
		offline = Bitmap.getBitmapResource("not_talking.jpg");
		list = new Vector();		
		listfield = new ListField();
		callback = new ListCallback();
		listfield.setCallback(callback);
		add(listfield);
		listfield.setChangeListener(selectServer);
		initializeList();
		reloadList();
	}
	
	private void reloadList()
	{
		listfield.setSize(list.size());
	}
	
	private void initializeList()
	{
		serverlist = new ServerList();
		for(int i = 0; i < serverlist.serverCount(); i++)
		{
			Server server = serverlist.printServers(i);
			list.addElement(server);
		}
	}
	
	public boolean onClose()
	{
		Connection.logout();
    	close();
		return true;
	}
	//What I tried
	FieldChangeListener selectServer = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			int index = listfield.getSelectedIndex();
			Server server = (Server) callback.get(listfield, index);
			try {
				Connection.UseServer(server.getid());
			} catch (ConnectionClosedException e) {
				Dialog.alert("Connection closed");
			} catch (IOException e) {
				Dialog.alert(e.getMessage());
			}
			main.ChangeScreen("ServerUser");
		}
	};
	
	private class ListCallback implements ListFieldCallback
	{

	    public void drawListRow(ListField listfield, Graphics g, int index, int y, int w) 
	    { 
	    	Server server = (Server) list.elementAt(index);
	        String text = server.toString();
			if(server.isOnline())
			{
				g.drawBitmap(new XYRect(0, 13, 15, 15), online, 0, 0);
			}
			else
			{
				g.drawBitmap(new XYRect(0, 13, 15, 15), offline, 0, 0);
			}
			g.drawText(text, 20, y, 0, w);
	    } 

		public Object get(ListField listField, int index) {
			return list.elementAt(index);
		}

		public int getPreferredWidth(ListField listField) {
			return getWidth();
		}

		public int indexOfList(ListField listField, String prefix, int start) {
			return list.indexOf(prefix, start);
		}

	}
}

 I hope you can help me.

 

Taxos

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: ListField action listener

welcome to the support forums.

i would suggest that you overwrite navigationclick (and maybe keychar if you want enter, backspace etc to do something).

Please note that blocking operations, such as networking, should be executed on a separate thread. Don't put this code directly into the event handler.
----------------------------------------------------------
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
Taxos
Posts: 9
Registered: ‎06-08-2011
My Device: Q10

Re: ListField action listener

Ok thank you :smileyhappy:

 

Btw the connection.useserver is to send a string to a server somewhere located. Its not something used to setup a connection or anything.

 

Thanks for your help.

 

Taxos

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: ListField action listener

if it throws an IOException it is a potentially blocking operation and should not be run on the event thread.
the simulator is very forgiving there, but a real device will kill your whole application (process not responding) at random.

take some minutes to watch this:
http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865
----------------------------------------------------------
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