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
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer
Accepted Solution

Issue in dynamically setting Text in LabelField

I have a timer task where i am changing LabelFields after certain interval of time.I am sending a vector of labelfield because its coming dynamically and adding to the screen.Below is my code 

package mypackage;

import java.util.Hashtable;
import java.util.TimerTask;
import java.util.Vector;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.decor.BackgroundFactory;
import com.imimobile.app.util.Utils;
import com.imimobile.controls.CustomVerticalLayout;

class Slider extends TimerTask {
	int count=0;
	BitmapField bmp;
	Vector imageSet;
	int wd,ht;
	CustomVerticalLayout vfm;
	LabelField lb,lb1;
	Vector bmpfield,imageWd,imageHt;
	Hashtable resultsTable;
	int j=0;
	 public Slider(CustomVerticalLayout itemVfm,BitmapField bmp,LabelField lb,Vector bmpfield,Vector imgSet,Vector wd,Vector ht)
     {
		  this.vfm=itemVfm;
          this.bmpfield=bmpfield;
          imageWd=wd;
          this.bmp=bmp;
          this.lb=lb;
          this.lb1=lb;
          imageHt=ht;
          imageSet=imgSet;
     }
	public void run() {
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			public void run() {
				if(imageSet.size()==count){
					count=0;
				}
			resultsTable = (Hashtable) imageSet.elementAt(count);
			vfm.setBackground(BackgroundFactory.createBitmapBackground(Utils.resizeImage(Bitmap.getBitmapResource((String) resultsTable.get("image")),CustomVerticalLayout.mWidth,CustomVerticalLayout.mHeight)));
				for(int i=0;i<bmpfield.size();i++){
					if( bmpfield.elementAt(i) instanceof BitmapField){
					((BitmapField) bmpfield.elementAt(i)).setBitmap(Utils.resizeImage(Bitmap.getBitmapResource((String) resultsTable.get("image")),Integer.parseInt((String)imageWd.elementAt(i)),Integer.parseInt((String)imageHt.elementAt(i))));
					}
					else if( bmpfield.elementAt(i) instanceof LabelField){
					((LabelField) bmpfield.elementAt(i)).setText((String) resultsTable.get("title"));
					((LabelField) bmpfield.elementAt(i)).setText((String) resultsTable.get("description"));
					}
					}
			
				count++;
			}
		});
	}
}

 I am checking whether the vector "bmpfield" is and instance of LabelField and if yes than i am changing the text with setText method.But the problem is that it is picking the last attribute ei. Description for both the LabelField.I want that first one should set Title and next one should set Description.Kindly help

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Issue in dynamically setting Text in LabelField

Here are few ways to solve this:

 

1. Create 2 empty class which will extend LabelField: TitleLabelField & DescLabelField. Now you can check that instanceof TitleLabelField or DescLabelField and update the text accordingly.

 

2. Extend LabelField and add a type parameter to it. Now if the element is instance of your LabelField, check its type and update the text.

 

3. If you know the order of the fields is always the same, just update the text according to their order.

 

I guess there few more ways but these are the first which come to mind.

 

Hope that helps,

 

E.

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Issue in dynamically setting Text in LabelField

Thanx Maadani.Btw i have a custom labelfield class which is like this 

 

/*
* Copyright (c) 2011 Research In Motion Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.imimobile.controls;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.decor.BackgroundFactory;


public class ColoredLabelField extends LabelField
{ 	
	int _foregroundColor;
	int bgcol=0;
	int _foregroundColorFocus;
	private int _width, _height;
	Bitmap backImg=null;
	public ColoredLabelField( String label,int foregroundColor,int width,int height,Bitmap bgimg,int bgcolor,int style) {
		this( label, foregroundColor,foregroundColor,width,height,bgimg,bgcolor,style );
	}
	
	public ColoredLabelField( String label, int foregroundColor, int foregroundColorFocus,int width, int height,Bitmap bgimg,int bgcolor, long style ) {
		super( label, style);
		this._width = width;
		this._height = height;
		_foregroundColor = foregroundColor;
		_foregroundColorFocus = foregroundColorFocus;
		backImg=bgimg;
		bgcol=bgcolor;
		if(bgcol!=0){
		setBackground(BackgroundFactory.createSolidBackground(Color.RED));
		}
		if(backImg!=null){
			setBackground(BackgroundFactory.createBitmapBackground(backImg));	
		}

	}
	protected void layout(int maxwidth, int maxheight) {
		super.layout(maxwidth, maxheight);
		setExtent(_width,_height);
	}
	public int getPreferredWidth() {
		if(_width > 0)
			return _width;
		return super.getPreferredWidth();
	}
	protected void drawFocus(Graphics graphics, boolean on) {

	}
	public int getPreferredHeight() {
		if(_height > 0)
			return _height;
		return super.getPreferredHeight();
	}
	protected void paint( Graphics g ) 
    {
        int oldColour = g.getColor();
        try {
            g.setColor( g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? _foregroundColorFocus : _foregroundColor );
            super.paint( g );
           
        } finally {
            g.setColor( oldColour );
        }
    }
}

 From my caller portion i am actually sending a vector of CustomLabelField

 

Now i tried doing this

 

if( bmpfield.elementAt(i) instanceof ColoredLabelField){
					((ColoredLabelField) bmpfield.elementAt(i)).setText((String) resultsTable.get("title"));
					((ColoredLabelField) bmpfield.elementAt(i)).setText((String) resultsTable.get("description"));
					}

 but the problem persists.Where i am doing wrong?

 

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Issue in dynamically setting Text in LabelField

The strange thing is that i use hard coded value 0 &1 insead of i it works

 

if( bmpfield.elementAt(i) instanceof ColoredLabelField){
					((ColoredLabelField) bmpfield.elementAt(0)).setText((String) resultsTable.get("title"));
					((ColoredLabelField) bmpfield.elementAt(1)).setText((String) resultsTable.get("description"));
					}

 But if i use i than it only takes Description

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Issue in dynamically setting Text in LabelField

You are setting the same text twice!

 

Add type member to your class and two constants: 0 for title & 1 for description.

 

Now, in your code do this:

 

if( bmpfield.elementAt(i) instanceof ColoredLabelField){

if((ColoredLabelField) bmpfield.elementAt(i)).type == 0) ((ColoredLabelField) bmpfield.elementAt(i)).setText((String) resultsTable.get("title"));
else ((ColoredLabelField) bmpfield.elementAt(i)).setText((String) resultsTable.get("description")); }
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Issue in dynamically setting Text in LabelField

Type Member means i have to add a Parameter called Type?

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Issue in dynamically setting Text in LabelField

Yes.

 

You would also would need to decide how/when to set its value (constructor/setter).

 

E.

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Issue in dynamically setting Text in LabelField

((ColoredLabelField) bmpfield.elementAt(i))._type== 0)

 This statement throwing me compiler error.But if i do "

ColoredLabelField._type=0

"

It succeeds.Now the thing is that i have taken a type parameter in my Custom Label Field class and assign it like this

 

public ColoredLabelField( String label,int foregroundColor,int width,int height,Bitmap bgimg,int bgcolor,int style,int type) {
		this( label, foregroundColor,foregroundColor,width,height,bgimg,bgcolor,style );
		_type=type;
	}

 so when the second labeldfield gets added to the screen the value of _type will be 1

 

Than when i check it from TimerTask class

 

 if(ColoredLabelField._type== 0)
					((ColoredLabelField) bmpfield.elementAt(i)).setText((String) resultsTable.get("title"));
						  else
						  ((ColoredLabelField) bmpfield.elementAt(i)).setText((String) resultsTable.get("description"));
					}

 It is always printing the Description 

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Issue in dynamically setting Text in LabelField

Just to rectify myself 

((ColoredLabelField) bmpfield.elementAt(i))._type== 0

 its not showing error bt it says "It should be accessed in a static way"

Highlighted
Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Issue in dynamically setting Text in LabelField

Remove the static keyword