BlackBerry listfield development [closed] - blackberry

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Can anybody suggest me how to create a clickable list field in blackberry such that on clicking one item a new screen must appear.

I've done a similar work with VerticalFieldManager and custom extended Field on JDE 5.0.
I assume that you have a list of objects that you wanted to display on main screen.
First; create a class which extends Field for your list's item, override its paint method, layout method and otuher events as your requirements.
Next, create a main screen that you wanted to show the list. Once you have populated your object list, in a loop, pass each object model to previously created field's constructor. Then add fieldChanged event to that field and add it to verticalFieldManager.
You need to override the events (like fieldChanged event) as you want to click on it and display its detail on another screen.
Finally, create a detail screen that takes required arguments to display your list item's object detail. On fieldChanged event of your main screen implementation, pass your object to detail screen and push the detail screen.
Also, this approach may be useful for you.
Example:
custom field:
public class CListItemField extends Field {
private CListItemModel model;
public CListItemField(CListItemModel _model, long style) {
super(style);
this.model = _model;
}
public CListItemModel getModel() {
return this.model;
}
// overrides
public int getPreferredHeight() {
//return custom height
}
public int getPreferredWidth() {
//return custom width
}
protected void layout(int width, int height) {
setExtent(Math.min(width, getPreferredWidth()), getPreferredHeight());
}
protected void paint(Graphics g) {
//custom paint stuff (borders, fontstyle, text position, icons etc.)
if (isFocus()) {
//focused item display settings
} else {
//item display settings
}
}
protected void drawFocus(Graphics graphics, boolean on) {
}
public boolean isFocusable() {
return true;
}
protected void onFocus(int direction) {
super.onFocus(direction);
invalidate();
}
protected void onUnfocus() {
super.onUnfocus();
invalidate();
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time) {
//send key event to listener
if (character == Keypad.KEY_ENTER) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}
list screen:
public class ScreenListbox extends MainScreen implements FieldChangeListener, FocusChangeListener {
private VerticalFieldManager verticalField;
private Vector listItemVector;
public ScreenOttoInbox(String title) {
super(title, Manager.NO_VERTICAL_SCROLL);
setData();
setComponents();
}
private void setData() {
//populate listItemVector according to your business (ie. read json response then parse it and collect it to a vector)
}
public void setComponents() {
verticalField = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
setListContent(verticalField, listItemVector);
add(verticalField);
}
private void setListContent(VerticalFieldManager field, Vector vector) {
try {
int vlen = vector.size();
for (int i = 0; i < vlen; i++) {
CListItemModel model = (CListItemModel) vector.elementAt(i);
CListItemField itemField = new CListItemField(model, Field.FOCUSABLE | Field.ACTION_INVOKE);
itemField.setChangeListener(this);
itemField.setFocusListener(this);
field.add(itemField);
}
} catch (Exception ex) { }
}
protected boolean onSavePrompt() {
return true;
}
public void fieldChanged(Field field, int context) {
//custom field's click/touch event handler
CListItemField itemField = (CListItemField) field;
ScreenItemDetail scrDetail = new ScreenItemDetail(itemField.getModel());
ScreenUtil.pushScreenWithLoader(scrDetail,true);
}
protected void onDisplay() {
super.onDisplay();
}
}

Create a class like below.
import java.util.Vector;
import net.rim.device.api.collection.util.SparseList;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
public class CListCallback implements ListFieldCallback {
private String[] resource;
private int rgb=Color.BLACK;
Vector elements;
Bitmap arraow;
public CListCallback(String[] resources){
this.resource=resources;
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
String text=(String) get(listField, index);
graphics.setColor(rgb);
graphics.drawText(text,60,y+25);
graphics.drawLine(0, y+59, DConfig.disWidth, y+59);
}
public Object get(ListField listField, int index) {
return resource[index];
}
public int getPreferredWidth(ListField listField) {
return DConfig.disWidth+10;
}
public int indexOfList(ListField listField, String prefix, int start) {
return -1;
}
}
And use above class in MainScreen class.
CListCallback clmenu=new CListCallback(arrayitems);
final ListField lf = new ListField(arraymenu.length) {
protected boolean keyChar(char character, int status, int time) {
if (character == Keypad.KEY_ENTER) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
protected boolean navigationUnclick(int status, int time) {
fieldChangeNotify(0);
return true;
}
};
lf.setCallback(clmenu);
lf.setRowHeight(60);
lf.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context)
{
int index=lf.getSelectedIndex();
UiApplication.getUiApplication.pushScreen(newNewScreen(imgarray[index]));
}
});
add(lf);
Thats it.it will work

Related

Invoking navigationClick from navigationClick 'father' in Blackberry

Edited
...
I´m trying to deploy a new component in my BlackBerry app. I have a new class that extends of HorizontalFieldManager, where I instanced a new ListField. Inside of this, I overwrote the navigationClick method and I open a popup. The name of this component is CustomComponentHorizontal. I use this component in other component customized, its name is CustomComponentVertical. This is the code:
In Main.java:
...
CustomComponentVertical ccv=new CustomComponentVertical();
...
add(ccv);
In CustomComponentVertical
...
CustomComponentHorizontal cch=new CustomComponentHorizontal{
...
protected boolean navigationClick(int status, int time) {
//Should I do something here??
return super.navigationClick(status, time);
}
};
...
add(cch);
In CustomComponentHorizontal:
public class CustomComponentHorizontal extends HorizontalFieldManager {
ListField choiceField=null;
PopupScreen popup=null;
public CustomComponentHorizontal (){
choiceField = new ListField(){
public boolean navigationClick(int status, int time) {
Field focus = UiApplication.getUiApplication().getActiveScreen().getLeafFieldWithFocus();
if (focus instanceof ListField) {
popup = new ChoicePopupScreen(10, 50, choices);
popup.setChoice(choices);
UiApplication.getUiApplication().pushScreen(popup);
setPopup(popup);
return super.navigationClick(status, time);
}
};
}
}
}
My purpose is that when I click in my component, it is launched the navigationClick of listfield. When I put the focus in my component and click with trackpad, the popup no open. But, If I touch the screen over the component, and then I click with trackpad, popup open.
How could I open the popup from component without to use the touch event?
Thank you very much.
I copy pasted your code. There were some errors, and your callback was missing. So I fixed it up a bit and created a simple callback to test with, and the navigationClick fires. Hopefully this helps, let me know if there is a different issue I didn't understand.
public CustomComponent()
{
elements = new Vector();
elements.addElement("Element 0");
elements.addElement("Element 1");
elements.addElement("Element 2");
elements.addElement("Element 3");
elements.addElement("Element 4");
elements.addElement("Element 5");
choiceField = new ListField(elements.size())
{
public boolean navigationClick(int status, int time)
{
int index = getSelectedIndex();
Dialog.inform((String) elements.elementAt(index));
return true;
}
};
choiceField.setCallback(new ListFieldCallback()
{
public int indexOfList(ListField listField, String prefix, int start)
{
return elements.indexOf(prefix, start);
}
public int getPreferredWidth(ListField listField)
{
return Display.getWidth();
}
public Object get(ListField listField, int index)
{
return elements.elementAt(index);
}
public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width)
{
String obj = (String) get(listField, index);
graphics.setColor(0x000000);
graphics.drawText(obj, 0, y, 0, width);
}
});
add(choiceField);
}

Blackberry navigationClick invoke problems

Eclipse keeps telling me about invoke problems. This is the message i get as can be seen below. Please help me resolve this problem. What code should i put to get rid of the invoke problem. thanks.
Warning!: method 'parsepack.xmlparsing.navigationClick(int,int)' not invoked.
Warning!: method 'parsepack.xmlparsing.insert(String,int)' not invoked.
here is the method navigationClick()
protected boolean navigationClick(int status, int time)
{
try
{
//navigate here to another screen
ui.pushScreen(new ResultScreen());
}
catch(Exception e)
{
System.out.println("Exception:- : navigationClick() "+e.toString());
}
return true;
}
here is the method insert()
public void insert(String toInsert, int index)
{
listElements.addElement(toInsert);
}
here is the class xmlparsing.java
package parsepack;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class xmlparsing extends UiApplication implements ListFieldCallback, FieldChangeListener
{
public static void main(String[] args)
{
xmlparsing app = new xmlparsing();
app.enterEventDispatcher();
}
public long mycolor ;
Connection _connectionthread;
private static ListField _list;
private static Vector listElements = new Vector();
public MainScreen screen = new MainScreen();
VerticalFieldManager mainManager;
VerticalFieldManager subManager;
UiApplication ui = UiApplication.getUiApplication();
public xmlparsing()
{
super();
pushScreen(screen);
final Bitmap backgroundBitmap = Bitmap.getBitmapResource("blackbackground.png");
mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
public void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, Display.getWidth(),Display.getHeight(),backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
subManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR )
{
protected void sublayout( int maxWidth, int maxHeight )
{
int displayWidth = Display.getWidth();
int displayHeight = Display.getHeight();
super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};
screen.add(mainManager);
_list = new ListField()
{
public void paint(Graphics graphics)
{
graphics.setColor((int) mycolor);
super.paint(graphics);
}
};
mycolor = 0x00FFFFFF;
_list.invalidate();
_list.setEmptyString("* Feeds Not Available *", DrawStyle.HCENTER);
_list.setRowHeight(50);
_list.setCallback(this);
mainManager.add(subManager);
listElements.removeAllElements();
_connectionthread = new Connection();
_connectionthread.start();
}
protected boolean navigationClick(int status, int time)
{
try
{
//navigate here to another screen
ui.pushScreen(new ResultScreen());
}
catch(Exception e)
{
System.out.println("Exception:- : navigationClick() "+e.toString());
}
return true;
}
private class Connection extends Thread
{
public Connection()
{
super();
}
public void run() {
Document doc;
StreamConnection conn = null;
InputStream is = null;
try {
conn = (StreamConnection) Connector.open("http://imforchange.org/international-movement-for-change/testing/data.xml"+";deviceside=true");
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
docBuilderFactory.setCoalescing(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
is = conn.openInputStream();
doc = docBuilder.parse(is);
doc.getDocumentElement().normalize();
NodeList list = doc.getElementsByTagName("eventName");
for (int i = 0; i < list.getLength(); i++)
{
Node textNode = list.item(i).getFirstChild();
listElements.addElement(textNode.getNodeValue());
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ignored) {
}
}
if (conn != null) {
try {
conn.close();
}
catch (IOException ignored) {
}
}
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
_list.setSize(listElements.size());
subManager.add(_list);
screen.invalidate();
}
});
}
}
public void drawListRow(ListField list, Graphics g, int index, int y, int w)
{
String tes = (String)listElements.elementAt(index);
int yPos = 0+y;
g.drawLine(0, yPos, w, yPos);
g.drawText(tes, 5, 15+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 insert(String toInsert, int index) {
listElements.addElement(toInsert);
}
public void fieldChanged(Field field, int context) {
}
}
ResultScreen.java
package parsepack;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class ResultScreen extends MainScreen {
public ResultScreen() {
LabelField resultLabel = new LabelField("Result: ");
add(resultLabel);
}
}
First of all, those messages you're seeing are warnings, not errors. Warnings are not always a problem, but sometimes they are. In this case, I think that at least the first message is a problem, so you'll want to fix it.
1) First, the navigationClick() method. The compiler is telling you that you have an implementation for the method navigationClick() that is never called from any of your code, or by the BlackBerry OS infrastructure. That's probably not what you want. Normally, navigationClick() is a method that you override in a class you write that extends one of the BlackBerry Field classes. For example, a ButtonField subclass. navigationClick() will be called, in that case, when the button is clicked.
But, you placed your navigationClick() method in the main UIApplication subclass. That's not what you want. You need that method to override the navigationClick() method in a field class. I'm not sure which field you want to have call this method when the user clicks. But, for example, you might do something like this:
_list = new ListField()
{
public void paint(Graphics graphics)
{
graphics.setColor((int) mycolor);
super.paint(graphics);
}
protected boolean navigationClick(int status, int time)
{
try
{
//navigate here to another screen
ui.pushScreen(new ResultScreen());
}
catch(Exception e)
{
System.out.println("Exception:- : navigationClick() "+e.toString());
}
return true;
}
};
That would make navigationClick() get called when your list is clicked, and it will get rid of the warning.
2) Regarding the warning about insert(), that's just because you're not using it anywhere. It look like you've added that method to allow code outside the xmlparsing class to be able to insert items into the list. Maybe that's what you want, but you just haven't yet written the other code to use that method. I see you having at least a few choices:
remove or comment out the insert() method until you need it. this will get rid of the warning.
add some more code that does use this method.
ignore the warning, just making sure you understand that the warning is telling you that you have some code that is not (yet) necessary, since it's unused
you can suppress warnings in Java programs, by doing this, or this, in the Eclipse preferences, if you know the warning is not a problem, and you don't want excessive warnings to hide real problems. I normally don't recommend this. Usually, it's better to fix the warning, than to hide it.

Java Blackberry Refresh Field Label

I have a main class and a screen class. I have a button that launches a datepicker. When the datepicker is closed I need the label of the button to be refreshed with the selected value. How should I do that?
I tried with invalidate, creating a CustomButtonField that implements different onFocus, onUnFocus, and some other stuff but I guess I implemented them in a wrong way...
My two clases are these ones...
Main...
public class TestClass extends UiApplication
{
public static Calendar alarm1time = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
public static void main(String[] args)
{
TestClass testClass = new TestClass ();
testClass.enterEventDispatcher();
}
public TestClass()
{
pushScreen(new TestClassScreen());
}
}
Screen...
public final class TestClassScreen extends MainScreen
{
public TestClassScreen()
{
ButtonField alarm1 = new ButtonField("Alarm : " + TestClass.alarm1time.get(Calendar.HOUR_OF_DAY) + ":" + TestClass.alarm1time.get(Calendar.MINUTE) ,ButtonField.FOCUSABLE)
{
public boolean navigationClick (int status , int time)
{
datePicker(1);
return true;
}
};
setTitle("Test Alarm");
add(new RichTextField(" "));
add(alarm1 );
}
public void datePicker(final int alarmNumber)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
DateTimePicker datePicker = null;
datePicker = DateTimePicker.createInstance(TestClass.alarm1time, null, "HH:mm");
if(datePicker.doModal())
{
Calendar cal = datePicker.getDateTime();
TestClass.alarm1time = cal;
//Here I need the label to be refreshed, after the datePicker is Ok
}
}
});
}
}
I found one way in the Blackberry Development Forum...
public boolean navigationClick (int status , int time)
{
datePicker(1, this);
return true;
}
public void datePicker(final int alarmNumber, final ButtonField buttonToUpdate)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
DateTimePicker datePicker = null;
datePicker = DateTimePicker.createInstance(TestClass.alarm1time, null, "HH:mm");
if(datePicker.doModal())
{
Calendar cal = datePicker.getDateTime();
TestClass.alarm1time = cal;
buttonToUpdate.setLabel(....)
}
}
});
}
A more usual way would be to have change listener listen for Button Clicks and do similar processing in there.
I think, this helps you:
public class Def extends MainScreen
{
ButtonField show;
LabelField label;
String str="";
ObjectChoiceField choiceField;
public Def()
{
createGUI();
}
private void createGUI()
{
String st_ar[]={"Date Picker"};
choiceField=new ObjectChoiceField("Select Date: ", st_ar)
{
protected boolean navigationClick(int status, int time)
{
DateTimePicker datePicker = DateTimePicker.createInstance( Calendar.getInstance(), "yyyy-MM-dd", null);
datePicker.doModal();
Calendar calendar=datePicker.getDateTime();
str=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.YEAR);
return true;
}
};
add(choiceField);
label=new LabelField("",Field.NON_FOCUSABLE);
add(label);
show=new ButtonField("Show");
show.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
label.setText("Time is: "+str);
}
});
add(show);
}
}

Touch Event in Blackberry?

I am developing one blackerry application both touch and non-touch device. I am using custom button in my app. This is my code
package CustomControls;
import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*;
public class ImageButton extends Field {
private int width;
private int height;
private Bitmap focusImage;
private Bitmap unfocusImage;
private boolean focusFlag=false;
private Bitmap image;
private String label;
private Font font;
public ImageButton()
{
}
public ImageButton(Bitmap focusImage,Bitmap unfocusImage,int width,int height,long style)
{
super(style);
label="";
this.focusImage=focusImage;
this.unfocusImage=unfocusImage;
image=unfocusImage;
this.width=width;
this.height=height;
}
public ImageButton(String label,Font font,Bitmap focusImage,Bitmap unfocusImage,int width,int height,long style)
{
super(style);
this.label=label;
this.font=font;
this.focusImage=focusImage;
this.unfocusImage=unfocusImage;
image=unfocusImage;
this.width=width;
this.height=height;
}
public int getPreferredHeight()
{
return height;
}
public int getPreferredWidth()
{
return width;
}
protected void onFocus(int direction)
{
image=focusImage;
invalidate();
}
protected void onUnfocus()
{
image=unfocusImage;
invalidate();
}
public void setChangeImage(Bitmap fImage,Bitmap uImage)
{
focusImage=fImage;
unfocusImage=uImage;
image=fImage;
invalidate();
}
protected void drawFocus(Graphics graphics, boolean on)
{
}
protected void layout(int width, int height)
{
setExtent(Math.min( width, getPreferredWidth()),Math.min(height, getPreferredHeight()));
}
protected void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, getWidth(),getHeight(), image, 0, 0);
if(label.length()>0)
{
graphics.setFont(font);
graphics.drawText(label,(width-(label.length()*2))/2,(height-font.getHeight()));
}
}
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(1);
return true;
}
protected void fieldChangeNotify(int context)
{
try
{
this.getChangeListener().fieldChanged(this,context);
}
catch (Exception exception)
{
System.out.println("==> Exception in Touch "+exception.toString());
}
}
protected boolean navigationMovement(int dx, int dy, int status,int time)
{
return true;
}
protected boolean touchEvent(TouchEvent message)
{
if (TouchEvent.CLICK == message.getEvent())
{
FieldChangeListener listener = getChangeListener();
if (null != listener)
listener.fieldChanged(this, 1);
}
return super.touchEvent(message);
}
protected boolean trackwheelRoll(int dir, int status, int time)
{
return true;
}
public void setBounds(int xPosition,int yPosition)
{
FieldPosition.setXPosition(this,xPosition);
FieldPosition.setYPosition(this,yPosition);
} }
I don't have problem in testing by using Simulator. But i am not able to navigate to button in Real device. I am using 9780 Blackberry bold device. I dont know where the problem occur
Try to do the following:
1). Remove this stuff:
protected boolean navigationMovement(int dx, int dy, int status,int time)
{
return true;
}
protected boolean trackwheelRoll(int dir, int status, int time)
{
return true;
}
2). Make sure your field IS focusable. As the docs say:
If you want your field to receive the
focus, then you must override
isFocusable to return true.
P.S. Actually you don't need to override the touchEvent(TouchEvent message). Check my another post on this.

how to add our menu item in messenger of blackberry

I want to add mymenuitem on smssending menulist,
How could I do that?
You just override the makeMenu method and add whatever you want.
protected void makeMenu(Menu menu, int instance) {
menu.add(getExitItem(0,0));
}
public MenuItem getExitItem(int ordinal, int priority) {
return new MenuItem("Close", ordinal, priority) {
public void run() {
System.exit(0);
}
};
}

Resources