06-08-2011 07:13 AM
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 ![]()
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
Solved! Go to Solution.
06-08-2011 07:20 AM
06-08-2011 07:56 AM
Ok thank you ![]()
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
06-08-2011 08:04 AM