06-17-2011 08:12 AM
Hi ,
Am doing an application in which i have some list of items shown in the list format one by one .
The problem is when i select an item from the list am not able to move to the next page. Can anyone please say me
where i went wrong.
[CODE]
class ListFieldScreen extends MainScreen implements FieldChangeListener {
private ListField _listField;
private Vector _listElements;
int listFieldIndex = -1;
public ListFieldScreen() {
setTitle("List Field Sample");
_listElements = new Vector();
_listField = new ListField();
ListCallback _callback = new ListCallback();
_listField.setCallback(_callback);
_listField.setRowHeight(45);//45 is used for setting the row Height
_listField.setChangeListener(this);
add(_listField);
initializeList();
//List Field Starts :
_listField = new ListField(_listElements.size()) {
protected void drawFocus(Graphics graphics, boolean on) {
System.out.println("The Automobile testing Inside ListField............");
}
//In the below either use TrackWheel or Navigation click :
/* protected boolean trackwheelClick(int status, int time) {
System.out.println("The Automobile testing Inside ListField............");
listFieldIndex = _listField.getSelectedIndex();
if (listFieldIndex < 0) {
listFieldIndex = 0;
}
try {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
//UiApplication.getUiApplication().pushScreen(new LiveScreen());
// UiApplication.getUiApplication().popScreen(getActi
}
});
}
catch (Exception e) {
}
return true;
} */
protected boolean navigationClick(int status, int time) {
listFieldIndex = _listField.getSelectedIndex();
if (listFieldIndex < 0) {
listFieldIndex = 0;
UiApplication.getUiApplication().pushScreen(new LiveScreen());
}
try {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(new LiveScreen());
// UiApplication.getUiApplication().popScreen(getActi
}
});
}
catch (Exception e) {
}
return true;
}
};
// ListField ends
}
private void initializeList() {
String itemOne = "List item One";
String itemTwo = "List item Two";
//_listElements is a vector
_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) {
//Giving the data stored in the vector to a String say "Text".
String text = (String)_listElements.elementAt(index);
System.out.println("The text Value is.......************........"+ text);
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();
}
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
}
}
[/CODE]
In the above case i tried using trackwheelClick and navigationClick . But still i am not able to.
please help me. its very Urgent
With Regards,
Sunil
12-07-2012 01:32 AM
HI,
You can use the following code for moving the one item to another:
Following is the code:
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.component.ObjectListField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManag
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class ListTestScreen extends MainScreen {
private VerticalFieldManager mainManager = new VerticalFieldManager( USE_ALL_WIDTH |FIELD_HCENTER);
public ListTestScreen() {
mainManager.setBackground(BackgroundFactory.create
LabelField lblTitle = new LabelField("FIFA World Cup Country List:",FIELD_HCENTER);
lblTitle.setBackground(BackgroundFactory.createSol
XYEdges edges = new XYEdges(2, 2, 2, 2);
lblTitle.setBorder(BorderFactory.createRoundedBord
mainManager.add(lblTitle);
mainManager.add(new MyList());
LabelField lblOther = new LabelField("Some Other Field",FIELD_HCENTER);
lblOther.setBackground(BackgroundFactory.createSol
lblOther.setBorder(BorderFactory.createRoundedBord
mainManager.add(lblOther);
add(mainManager);
}
}
class MyList extends VerticalFieldManager implements ListFieldCallback {
private int mouseDownY, mouseUpY;
private static int listRowHeight = 30;
private int mouseDownRowIndex, mouseUpRowIndex;
int firstSelectedRow=-1, secondSelectedRow=-1;
private int touchX;
private int touchY;
private int listVisibleHeight = 250;
private int listVisibleWidth = 200;
private boolean showShadow = false;
private ObjectListField list;
private Object[] listData = new Object[] { "South Africa", "Argentina", "Germany",
"Australia","Nigeria","Greece","England","Italy","
"Mexico","Cameroon","Denmark","Portugal","Netherla
"South Korea","USA","Algeria","Slovenia","Japan","Switzer
public MyList() {
super(VERTICAL_SCROLL|FIELD_HCENTER);
init();
setBackground(BackgroundFactory.createSolidBackgro
XYEdges edges = new XYEdges(6, 6, 6, 6);
setBorder(BorderFactory.createRoundedBorder(edges, 0xB0B0B0, Border.STYLE_FILLED));
}
private void init() {
list = new ObjectListField(FOCUSABLE|Field.FIELD_HCENTER);
list.setRowHeight(listRowHeight);
list.setCallback(this);
list.set(listData);
add(list);
}
private void changeRowPosition(int fromRow, int toRow) {
Object temp = listData[fromRow];
int increment = (fromRow<toRow)?1:-1, i;
for(i = fromRow+increment; i != toRow+increment ; i=i+increment) {
listData[i-increment] = listData[i];
}
listData[toRow] = temp;
invalidate();
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
graphics.setFont(Font.getDefault().derive(Font.PLA
graphics.drawText(listData[index].toString(), 25, y + 4);
}
public Object get(ListField listField, int index) {
return listData[index];
}
public int getPreferredWidth(ListField listField) {
return getPreferredWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
return 0;
}
public int indexOfRowAt(int posY) {
int index =(int) Math.floor(posY / listRowHeight * 1.0);
return index;
}
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(listVisibleWidth, listVisibleHeight);
setExtent(listVisibleWidth, listVisibleHeight);
}
protected boolean keyChar(char ch, int status, int time) {
if(ch==Characters.SPACE) {
if(firstSelectedRow ==-1) {
firstSelectedRow = list.getSelectedIndex();
return true;
} else {
secondSelectedRow = list.getSelectedIndex();
if(firstSelectedRow == secondSelectedRow) {
firstSelectedRow = secondSelectedRow = -1;
invalidate();
return true;
} else {
changeRowPosition(firstSelectedRow, secondSelectedRow);
firstSelectedRow = secondSelectedRow = -1;
return true;
}
}
}
return super.keyChar(ch, status, time);
}
protected boolean navigationMovement(int dx, int dy, int status, int time) {
invalidate();
return super.navigationMovement(dx, dy, status, time);
}
protected boolean touchEvent(TouchEvent message) {
int eventCode = message.getEvent();
// Get the screen coordinates of the touch event
touchX = message.getX(1);
touchY = message.getY(1)+getVerticalScroll();
if(eventCode == TouchEvent.DOWN) {
mouseDownY = touchY;
mouseDownRowIndex = indexOfRowAt(mouseDownY);
showShadow = true;
invalidate();
return true;
}
else if(eventCode == TouchEvent.UP) {
showShadow = false;
mouseUpY = touchY;
mouseUpRowIndex = indexOfRowAt(mouseUpY);
if(mouseDownRowIndex != mouseUpRowIndex) {
changeRowPosition(mouseDownRowIndex, mouseUpRowIndex);
mouseDownRowIndex = mouseUpRowIndex = -1;
invalidate();
return true;
}
mouseDownRowIndex = mouseUpRowIndex = -1;
invalidate();
return true;
} else if(eventCode == TouchEvent.MOVE) {
int index = indexOfRowAt(touchY-listRowHeight/2);
if(touchY-getVerticalScroll()<5) {
if(index > 0) {
index--;
}
} else if(touchY> (getPreferredHeight()-5)) {
if(index<listData.length-1) {
index++;
}
}
if(list.getSelectedIndex()!=index)
list.setSelectedIndex(index);
invalidate();
return true;
}
return super.touchEvent(message);
}
protected void paint(Graphics graphics) {
// Save the original color and transparency values for the graphics
int preColor = graphics.getColor();
int preAlpha = graphics.getGlobalAlpha();
if(firstSelectedRow!=-1) {
int y = firstSelectedRow*listRowHeight;
graphics.setColor(0x808080);
graphics.fillRect(0, y, getWidth(), listRowHeight);
}
// Reset the previous color and transparency values for this graphics
graphics.setColor(preColor);
graphics.setGlobalAlpha(preAlpha) ;
super.paint(graphics);
if(firstSelectedRow != -1) {
int index = list.getSelectedIndex();
int y = index*listRowHeight;
String shadowText = listData[firstSelectedRow].toString();
Font preFont = graphics.getFont();
Font smallFont = preFont.derive(Font.BOLD, preFont.getHeight()-5);
y += (listRowHeight-smallFont.getHeight())/2;
graphics.setFont(smallFont);
int shadowTextLength = smallFont.getAdvance(shadowText);
graphics.setColor(0xE0E0E0);
graphics.setGlobalAlpha(100) ;
graphics.fillRoundRect(getWidth()/2-2, y, shadowTextLength+4, smallFont.getHeight(), 10, 10);
graphics.setColor(0x303030);
graphics.setGlobalAlpha(170) ;
graphics.drawText(shadowText, getWidth()/2, y);
graphics.setFont(preFont);
}
// Drawing the first selected SHADOW TEXT
if(showShadow && mouseDownRowIndex != -1) {
String shadowText = listData[mouseDownRowIndex].toString();
Font preFont = graphics.getFont();
Font smallFont = preFont.derive(Font.BOLD, preFont.getHeight()-5);
graphics.setFont(smallFont);
int shadowTextLength = smallFont.getAdvance(shadowText);
graphics.setColor(0xE0E0E0);
graphics.setGlobalAlpha(100) ;
graphics.fillRoundRect(touchX+10, touchY-smallFont.getHeight()/2, shadowTextLength, smallFont.getHeight(), 10, 10);
graphics.setColor(0x303030);
graphics.setGlobalAlpha(170) ;
graphics.drawText(shadowText, touchX+10, touchY-smallFont.getHeight()/2);
graphics.setFont(preFont);
}
// Drawing the VERTICAL SCROLLBAR
graphics.setColor(0x606060);
graphics.drawLine(getWidth()-18, getVerticalScroll(), getWidth()-18,getVerticalScroll()+getHeight());
graphics.drawLine(getWidth()-17, getVerticalScroll(), getWidth()-17,getVerticalScroll()+getHeight());
graphics.drawLine(getWidth()-16, getVerticalScroll(), getWidth()-16,getVerticalScroll()+getHeight());
int listTotalHeight = listRowHeight * (listData.length+5);
int y = list.getSelectedIndex()*listRowHeight ;
int yScrollPosition = (int)Math.floor(((y*getHeight()*1.0)/listTotalHeig
graphics.fillRoundRect((getWidth()-17)-4, getVerticalScroll()+yScrollPosition, 8, listRowHeight, 2, 2);
}
}
12-07-2012 07:42 PM
Slow down, I think we should actually answer the question rather than supplying complicated code that doesn't clearly explain the way it is working, or more helpfully, why the original poster's code is not working.
The approach you are taking, which is overriding navigationClick, should work. In fact, because this method already runs on the Event Thread, you don't need the invokelater - you can just do a push. But it does not hurt.
Since that should work, the question is why doesn't it?
And the easiest why to figure that out is to put a break point in the method and the start and step through it. Also put a break point inside the invokelater run() method.
I would also beak this up:
UiApplication.getUiApplication().pushScreen(new LiveScreen());
to:
LiveScreen ls = new LiveScreen();
UiApplication.getUiApplication().pushScreen(ls);
The reason for doing this is simply to be able to put a break point on the second line, and so confirm that the constructor is working correctly.
Let us know how you get on.
There are some other options and points to make, but we can talk about these once you have this working.