Checkbox in CustomListField in Blackberry - blackberry

I am creating a CustomList. In each row I am having a image, some text fields and a checkbox. But i am not able to check and uncheck the checkbox.
I have gone through this link but cant solve my problem.
http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800345/How_To_-_Create_a_ListField_with_check_boxes.html?nodeid=1165752&vernum=0
this will add a menu item in the menu to change the checkbox status.
I want the normal checkbox behaviour.
Please help
this is my code
class CustomList extends ListField implements ListFieldCallback
{
public Vector rows;
private Bitmap p1;
int z = this.getRowHeight();
LabelField task,task2,task3;
CheckboxField checkbox[]= new CheckboxField[40];
int rowcount;
public CustomList(int rowcount,String text1,double text2,String type,Bitmap p)
{
super(0, ListField.MULTI_SELECT);
this.rowcount= rowcount;
setRowHeight(z*3);
setEmptyString("Hooray, no tasks here!", DrawStyle.HCENTER);
setCallback(this);
p1 = p;
rows = new Vector();
for (int x = 0; x < rowcount; x++)
{
checkbox[x]=new CheckboxField("",false,Field.USE_ALL_HEIGHT|Field.EDITABLE|Field.FIELD_VCENTER|Field.FOCUSABLE);
}
for (int x = 0; x < rowcount; x++)
{
TableRowManager row = new TableRowManager();
row.add(checkbox[x]);
row.add(new BitmapField(p1));
task= new LabelField("abc");
row.add(task);
task2=new LabelField("abc2");
row.add(task2);
task3= new LabelField("abc3");
row.add(task3);
rows.addElement(row);
}
setSize(rows.size());
}
public void drawListRow(ListField listField, Graphics g, int index, int y,int width)
{
CustomList list = (CustomList) listField;
TableRowManager rowManager = (TableRowManager) list.rows.elementAt(index);
rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}
private class TableRowManager extends Manager
{
public TableRowManager()
{
super(0);
}
// Causes the fields within this row manager to be layed out then
// painted.
public void drawRow(Graphics g, int x, int y, int width, int height)
{
layout(width, height);
setPosition(x, y);
g.pushRegion(getExtent());
subpaint(g);
g.setColor(0x00CACACA);
g.popContext();
}
protected void sublayout(int width, int height)
{
// set the size and position of each field.
}
public int getPreferredWidth()
{
return UIConstants.SCREEN_WIDTH;
}
public int getPreferredHeight()
{
return getRowHeight();
}
}
}
I am trying to implement listfield with checkboxes and i have seen its code in the knowledge base.
But i want that when i click on the checkbox it should be checked or unchecked onlyand when i click on the rest part of the row, i.e. anywhere except the checkbox then nothing should occur without checking the checkbox
How can i do that ?? Or i should do something else to do it.. Please suggest..

Do some thing like this where you are overriding your sublayOut method() in table row manager class
protected void sublayout(int width, int height)
{
Field field = getField(0);
layoutChild(field, Display.getWidth(), Display.getHeight());
setPositionChild(field, 10, 20);
Field field = getField(0);
layoutChild(field, Display.getWidth(), Display.getHeight());
setPositionChild(field, 10, 20);
//Set field
field = getField(1);
layoutChild(field, 120, fontHeight - 1 );
setPositionChild(field, 70, 05);
// set the list field
field = getField(2);
layoutChild(field, 150, fontHeight +10);
setPositionChild(field, 70, 25);
// set the field
field = getField(3);
layoutChild(field, (Display.getWidth()/6)-12, fontHeight + 10);
setPositionChild(field, 70, 45);
setExtent(preferredWidth, getPreferredHeight());
}
change the padding as your require ments.
And you can also check this link create a listField with check box. Thanks

boolean checked = false;
Vector box1 = new Vector();
for(int i=0;i<vector.size();i++){
FriendsRequestObject co_vec = (FriendsRequestObject)vector.elementAt(i);
String name=co_vec.getSender_name();
CheckboxField box = new CheckboxField(" "+name , checked, Field.USE_ALL_WIDTH){
public void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
};
box1.addElement(box);
box.setMargin(8, 0, 0, 5);
add(box);
}
then add a button. On the click listener of the button,
for(int d=0; d<box1.size(); d++){
CheckboxField box=(CheckboxField)box1.elementAt(d);
if(box.getChecked()==true){
FriendsRequestObject co_vec = (FriendsRequestObject)vector.elementAt(d);
String name_ = co_vec.getSender_name();
Dialog.alert(name_);// here you can implement your code
}
}

Related

Image gone After Focusing

This is before focus state. It work fine.
This is on focusing state. It work fine.
This is after focus state. It occurred problem where the image gone.
It works fine for the top right but top left image got problem.
Here is my custom VerticalFieldManager:
public class Custom_TopField extends HorizontalFieldManager implements
FieldChangeListener {
private Bitmap bg = Bitmap.getBitmapResource("header_bar.png");
private Bitmap download = Bitmap.getBitmapResource("btn_download.png");
private Bitmap downloadactive = Bitmap
.getBitmapResource("btn_download_active.png");
private Bitmap refresh = Bitmap.getBitmapResource("icon_refresh.png");
private Bitmap refreshactive = Bitmap
.getBitmapResource("icon_refresh_active.png");
private Bitmap back = Bitmap.getBitmapResource("btn_back.png");
private Bitmap backctive = Bitmap.getBitmapResource("btn_back_active.png");
private Bitmap news = Bitmap.getBitmapResource("icon_news.png");
private Bitmap newsactive = Bitmap
.getBitmapResource("icon_news_active.png");
private Custom_ButtonField downloadbtn, refreshbtn, backbtn, newsbtn;
private Custom_LabelField title;
Custom_TopField(final MainScreen mainscreen) {
Background background = BackgroundFactory.createBitmapBackground(bg);
setBackground(background);
title = new Custom_LabelField("东方日报", DrawStyle.ELLIPSIS
| LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER
| Field.FOCUSABLE, Color.WHITE) {
protected boolean navigationClick(int status, int time) {
Main.getUiApplication().pushScreen(new Main_AllLatestNews());
Main.getUiApplication().popScreen(mainscreen);
return true;
}
};
title.setFont(Font.getDefault().derive(Font.BOLD, 33));
add(title);
downloadbtn = new Custom_ButtonField(download, downloadactive,
downloadactive);
downloadbtn.setChangeListener(this);
add(downloadbtn);
refreshbtn = new Custom_ButtonField(refresh, refreshactive,
refreshactive);
refreshbtn.setChangeListener(this);
add(refreshbtn);
backbtn = new Custom_ButtonField(back, backctive, backctive);
backbtn.setChangeListener(this);
add(backbtn);
/*newsbtn = new Custom_ButtonField(news, newsactive, newsactive);
newsbtn.setChangeListener(this);
add(newsbtn);*/
}
protected void sublayout(int width, int height) {
Field field = getField(0);
layoutChild(field, 120, Font.getDefault().getHeight());
setPositionChild(field, (getPreferredWidth() - title.getWidth()) / 2,
15);
field = getField(1);
layoutChild(field, download.getWidth(), download.getHeight());
setPositionChild(field, getPreferredWidth()
- (download.getWidth() + 10),
getPreferredHeight() - (download.getHeight() + 5));
field = getField(2);
layoutChild(field, refresh.getWidth(), refresh.getHeight());
setPositionChild(field,
getPreferredWidth() - (refresh.getWidth() + 10),
getPreferredHeight() - (refresh.getHeight() + 5));
field = getField(3);
layoutChild(field, back.getWidth(), back.getHeight());
setPositionChild(field, 10, 5);
/*field = getField(4);
layoutChild(field, news.getWidth(), news.getHeight());
setPositionChild(field, 10, 5);*/
width = Math.min(width, getPreferredWidth());
height = Math.min(height, getPreferredHeight());
setExtent(width, height);
}
public int getPreferredHeight() {
return 70;
}
public int getPreferredWidth() {
return Display.getWidth();
}
public void paint(Graphics graphics) {
int rectHeight = getPreferredHeight();
int rectWidth = getPreferredWidth();
graphics.drawRect(0, 0, rectWidth, rectHeight);
super.paint(graphics);
}
public void fieldChanged(Field field, int context) {
if (field == downloadbtn) {
} else if (field == refreshbtn) {
} else if (field == backbtn) {
} else if (field == newsbtn) {
}
}
}
Here is custom button field
public class Custom_ButtonField extends ButtonField {
Bitmap mNormal;
Bitmap mFocused;
Bitmap mActive;
int mWidth;
int mHeight;
private int color = -1;
String text;
public Custom_ButtonField(Bitmap normal, Bitmap focused, Bitmap active) {
super(CONSUME_CLICK | Field.FOCUSABLE);
mNormal = normal;
mFocused = focused;
mActive = active;
mWidth = mNormal.getWidth();
mHeight = mNormal.getHeight();
setMargin(0, 0, 0, 0);
setPadding(0, 0, 0, 0);
setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
setBorder(VISUAL_STATE_ACTIVE,
BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
}
public Custom_ButtonField(String text, Bitmap normal, Bitmap focused,
Bitmap active, int color) {
super(CONSUME_CLICK | Field.FOCUSABLE);
this.color = color;
mNormal = normal;
mFocused = focused;
mActive = active;
mWidth = mNormal.getWidth();
mHeight = mNormal.getHeight();
setMargin(0, 0, 0, 0);
setPadding(0, 0, 0, 0);
setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
setBorder(VISUAL_STATE_ACTIVE,
BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
this.text = text;
}
protected void onFocus(int direction) {
super.onFocus(direction);
}
protected void onUnfocus() {
super.onUnfocus();
}
protected void paint(Graphics graphics) {
Bitmap bitmap = null;
switch (getVisualState()) {
case VISUAL_STATE_NORMAL:
bitmap = mNormal;
break;
case VISUAL_STATE_FOCUS:
bitmap = mFocused;
break;
case VISUAL_STATE_ACTIVE:
bitmap = mActive;
break;
default:
bitmap = mNormal;
}
graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
bitmap, 0, 0);
graphics.setFont(Font.getDefault().derive(Font.BOLD, 25));
graphics.setColor(color);
graphics.drawText(text, (mNormal.getWidth() - Font.getDefault()
.getAdvance(text)) / 2, ((mNormal.getHeight() - Font
.getDefault().getHeight()) / 2) + 10, DrawStyle.HCENTER
| DrawStyle.VCENTER);
}
public int getPreferredWidth() {
return mWidth;
}
public int getPreferredHeight() {
return mHeight;
}
protected void layout(int width, int height) {
setExtent(mWidth, mHeight);
}
}
This is the second problem just like this I've seen in the last couple weeks.
Background
To understand the solution, first you should understand the basic UI classes in BlackBerry.
First, we have the Field class. A Field is the base class of the normal UI components. If you write a UI component yourself, from scratch, then you would subclass Field:
public class MyWidget extends Field {
However, if there already exists a BlackBerry class that does almost what you need, and you just need to change its behaviour a bit, then you would subclass something else. For example:
public class MyButtonWidget extends ButtonField {
The same pattern exists for the Manager class. If you are writing a Manager from scratch, then extend Manager:
public class MyManager extends Manager {
which involves doing this, according to the BlackBerry docs:
Implementing your own layout manager
If you have particular needs, you
can implement your own manager. Extend the Manager class, and
implement sublayout, getPreferredWidth, and getPreferredHeight. For
efficiency, you may optionally override subpaint.
However, if an existing Manager subclass already does most of what you need, and you just want to customize it, then you might consider extending that subclass:
public class MyHorizontalManager extends HorizontalFieldManager {
In your case, your Custom_TopField is doing all of the required work for a fully custom Manager (see the highlighted quote above from the javadocs). So, there's not really any reason for you to extend HorizontalFieldManager. A HorizontalFieldManager is used when you just want to add() your fields, and have them all laid out horizontally. But, you do that explicitly in your sublayout() code. As it turns out, it looks like your logic is competing with the base class.
Solution
So, what you should do, is have your class just extend Manager:
public class Custom_TopField extends Manager implements FieldChangeListener {
If you do that, you will need to call a different super constructor. Something like this (you might want to pick different style constants depending on your needs):
Custom_TopField(final MainScreen mainscreen) {
super(Manager.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL);
Another alternative would be to simply not implement sublayout(), extend HorizontalFieldManager like you originally had, and then control layout with the child fields' margins and long style flags. But, since the solution I gave above requires only changing 2 lines of code, that's probably the easiest for you this time.
Other Problem(s)
I also noticed in your code, and screenshots, that the Download button doesn't show up. I don't know the exact size of all your png images, but if the refresh and download images are the same size, then your current logic is just laying out the refresh button right over the download button. So, the download button is hidden. That's probably not what you want?

custom field focus in blackberry

I created one customUi class in that im creating 3 fiels as 1 label and two button fileds....
so i m creating the customui and passing the filed names...
like
public class CustomTextField extends Manager {
Field field ;
//passing 3 names to the construcor
protected void sublayout(int width, int height) {
// TODO Auto-generated method stub
int fieldCount = getFieldCount();
int screenWidth = Display.getWidth();
int yOffset =0;
for(int i=0; i<fieldCount; i++)
{
field= getField(i);
layoutChild(field, screenWidth, 100 );
setPositionChild(field, (screenWidth-field.getWidth())>>1, yOffset);
yOffset += field.getHeight()+5;
}
setExtent(screenWidth, yOffset);
}
protected void paintBackground(Graphics g) {
if(field.isFocus())
{
//g.setBackgroundColor(Color.ALICEBLUE);
int oldColor = g.getColor();
g.setColor(Color.DARKBLUE);
g.drawRoundRect(0, 0, getWidth(), getHeight(), Graphics.ALL_ROUNDED_RECT_CORNERS, 60, 60);
g.setColor(oldColor);
}
}
Don't override paintBackground(Graphics g) inside of the manager. Override it inside of the child field class. It's responsibility of a child field how to present its focused state.

How to develop custom List Field in Blackberry

In my app i have to show list of items in list field when i click on specific item in the list field the background color for a particular row change to grey color.How to develop this type of custom list field in Blackberrry.Any one please give ideas.
Thank You
you should draw rectangle in listfield row which is selected.. some thing like this. Here i have done it for on focus..
public void drawListRow(ListField list, Graphics g, int index, int y,int w) {
if (g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
{
g.setBackgroundColor(0x00572000);
//g.setBackgroundColor();
g.clear();
//g.setColor(Color.BLACK);
// g.fillRect(0,list.getWidth(),list.getWidth(),80);
g.setColor(Color.ORANGE);
g.fillRect(94,y+0,400,30);
//g.setColor(0x000000);
g.setColor(Color.WHITE);
g.drawText(text, 95, y+10, (DrawStyle.LEFT ), w );
}
else
{
g.setColor(0x00906966);
g.fillRect(94,y+0,400,30);
g.setColor(Color.ORANGE);
g.drawText(text, 95, y+10, (DrawStyle.LEFT ), w );
}}
Try this ...
private class MyListField extends ListField{
//0,ListField.MULTI_SELECT
private boolean hasFocus = false;
public void onFocus(int direction){
hasFocus = true;
}
public void onUnfocus()
{
hasFocus = false;
super.onUnfocus();
invalidate();
}
public void paint(Graphics graphics)
{ int width = Display.getWidth();
//Get the current clipping region
XYRect redrawRect = graphics.getClippingRect();
if(redrawRect.y < 0)
{
throw new IllegalStateException("Error with clipping rect.");
}
//Determine the start location of the clipping region and end.
int rowHeight = getRowHeight();
int curSelected;
//If the ListeField has focus determine the selected row.
if (hasFocus)
{
curSelected = getSelectedIndex();
}
else
{
curSelected = -1;
}
int startLine = redrawRect.y / rowHeight;
int endLine = (redrawRect.y + redrawRect.height - 1) / rowHeight;
endLine = Math.min(endLine, getSize() - 1);
int y = startLine * rowHeight;
//Setup the data used for drawing.
int[] yInds = new int[]{y, y, y + rowHeight, y + rowHeight};
int[] xInds = new int[]{0, width, width, 0};
//Set the callback - assuming String values.
ListFieldCallback callBack = this.getCallback();
//Draw each row
for(; startLine <= endLine; ++startLine)
{
//If the line we're drawing is the currentlySelected line then draw the fill path in LIGHTYELLOW and the
//font text in Black.
if(startLine == curSelected){
graphics.setColor(Color.LIGHTYELLOW);
graphics.drawFilledPath(xInds, yInds, null, null);
graphics.setColor(Color.BLACK);
graphics.drawText((String)callBack.get(this, startLine), 0, yInds[0]);
}
else{
//Draw the odd or selected rows.
graphics.setColor(Color.LIGHTGREY);
graphics.drawText((String)callBack.get(this, startLine), 0, yInds[0]);
}
//Assign new values to the y axis moving one row down.
y += rowHeight;
yInds[0] = y;
yInds[1] = yInds[0];
yInds[2] = y + rowHeight;
yInds[3] = yInds[2];
}
//super.paint(graphics);
}
}
.
.
.
refer this [LINK] : http://berrytutorials.blogspot.com/2009/11/create-custom-listfield-change.html

BlackBerry Torch scrolling issue

I have created an app which is working fine on BlackBerry bold but when I installed it on Torch the screen is not scrolling..
I have used Manager with enabled vertical scrolling and added it to another master vertical field manager?
Did any one get this issue before ??
the code for the manager is below
public class TableManager extends Manager {
public int HEIGHT = 0;
public TableManager(int height) {
super(Manager.USE_ALL_WIDTH|Manager.VERTICAL_SCROLL);
HEIGHT = height;
}
public int getPreferredWidth() {
return Constants.width * 90 / 100;
}
public int getPreferredHeight() {
return HEIGHT;
}
protected void sublayout(int maxWidth, int maxHeight) {
int y = 150;
try{
int count = getFieldCount();
for (int i = 0; i < count; i++) {
Field field = getField(i);
layoutChild(field, field.getPreferredWidth(), field
.getPreferredHeight());
setPositionChild(field, (Constants.width - field
.getPreferredWidth()) >> 1, y);
y += field.getPreferredHeight();
}
setExtent(maxWidth, HEIGHT);
}catch(Exception e){
e.printStackTrace();
}
}
}
Try to add your TableManager to a master manager that is not vertically Scrollable.
You may also use a simple VerticalFieldManager with any Field inside set to FIELD_HCENTER instead of using this TableManager.

it displays only text,but not the image

i developed the code as below.in this i used listfield ,one bitmapfield and one label field,when i will run it ,it displays only text on the list field row,but not the image
i don't know where i did mistake,so,plz,any one help me to know where i did mistake
thanks for any help
class TaskListField extends MainScreen implements ListFieldCallback {
private Vector rows;
private Bitmap p1;
ListField list;
TableRowManager row;
public TaskListField() {
super();
list=new ListField() {
protected void drawFocus(Graphics graphics, boolean on) {
}
};
list.setRowHeight(40);
list.setEmptyString("Hooray, no tasks here!", DrawStyle.HCENTER);
list.setCallback(this);
p1 = Bitmap.getBitmapResource("res/images/10.png");
rows = new Vector();
for (int x = 1; x < 13; x++) {
row = new TableRowManager();
LabelField task = new LabelField("" + String.valueOf(x),
DrawStyle.ELLIPSIS);
row.add(task);
row.add(new BitmapField(p1));
rows.addElement(row);
}
list.setSize(rows.size());
add(list);
}
// ListFieldCallback Implementation
public void drawListRow(ListField listField, Graphics g, int index, int y,
int width) {
TableRowManager rowManager = (TableRowManager) rows
.elementAt(index);
rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}
private class TableRowManager extends Manager {
public TableRowManager() {
super(0);
}
public void drawRow(Graphics g, int x, int y, int width, int height) {
layout(width, height);
setPosition(x, y);
g.pushRegion(getExtent());
// Paint this manager's controlled fields.
subpaint(g);
g.setColor(0x00CACACA);
g.drawLine(0, 0, getPreferredWidth(), 0);
// Restore the graphics context.
g.popContext();
}
protected void sublayout(int width, int height) {
int preferredWidth = getPreferredWidth();
Field field = getField(0);
layoutChild(field, 30, 30);
setPositionChild(field, 0, 0);
field = getField(1);
layoutChild(field, 40, 25);
setPositionChild(field, 120, 10);
setExtent(preferredWidth, getPreferredHeight());
}
// The preferred width of a row is defined by the list renderer.
public int getPreferredWidth() {
return Graphics.getScreenWidth();
}
// The preferred height of a row is the "row height" as defined in the
// enclosing list.
public int getPreferredHeight() {
return list.getRowHeight();
}
}
public Object get(ListField listField, int index) {
// TODO Auto-generated method stub
return null;
}
public int getPreferredWidth(ListField listField) {
// TODO Auto-generated method stub
return 0;
}
public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return 0;
}
Are you sure the bitmap is not null? I would check that first -- perhaps it's not finding the resource.
Could the LabelField be taking the entire width of the screen (it does that on certain situations)? When you only set the image without setting the label, does the image show?
The image path is not necessary as you put it
p1 = Bitmap.getBitmapResource (" res/images/10.png ");
only needs
p1 = Bitmap.getBitmapResource (" 10.png ");

Resources