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
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel
Accepted Solution

Custom Filter field

[ Edited ]

I am trying to make a custom Edit field, with its own filter.

 

I already know about extending TextFilter and overriding validate and convert, but it;s giving me trouble. I also know about the whole FILTER_NUMERIC tag, as well as others, but I still can't get it to work.

 

My objective is to make a basic time validate field(This is the only way I want it done). The user user can't enter a number greater than sixty or less than five. I can't get it to work though. it has no errors but when I run it I get a runtime error. I know for one that the 60 is a string not a character but I'm lost.

 

Please, I'd love some help. Some source code would be much appreciated. I'm an utter noobasaurus!

Here is my code:

public static class TimeValidateField extends AutoTextEditField
{
	TimeValidateFilter timeVal=new TimeValidateFilter();
		
	TimeValidateField(String label ,String label1,int digits,int min,int max)
	{
		super(label,label1);
		this.setFilter(timeVal);
			
	}
		
	public class TimeValidateFilter extends TextFilter{

		public char convert(char character, int status) {
			if(!validate(character)){
				return 0;
			}
			else
			{				
				return character;
			}
		}
			

		public boolean validate(char character) {
				
			if(character > 60){
				return false;
			}else if(character < 1){
					return false;
			}else{
				return true;
			}
				
		}
			
	}
}

 Any suggetions please?

_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Contributor
Posts: 45
Registered: ‎12-20-2010
My Device: Not Specified

Re: Custom Filter field

Hi,

There is a method validate(AbstractString text) in TextFilter, try using that. Something like this:

 

public boolean validate(AbstractString text){

    if(text.leanght() >60){

      return false;

   } else if(text.lenght() < 1){

     return false;

   } else {

     return true;

    }

}

Highlighted
Developer
Posts: 263
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: Custom Filter field

In your case, why don't use NumericChoiceField (OS 5.0):

int iStartAt = 5;
int iEndAt = 60;
int iIncrement = 1;
int iSetTo = 10;
add(new NumericChoiceField("Numeric Drop-Down List", iStartAt, iEndAt, iIncrement, iSetTo));

 

--tom
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel

Re: Custom Filter field

[ Edited ]

Thanks bro the response man. Are you sure this will work though bro?

_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel

Re: Custom Filter field

Well I guess that that could work too. But do you have any suggestions that could help me do it the way I proposed, please?
_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Developer
Posts: 263
Registered: ‎05-18-2011
My Device: Curve 9300, BB10 Dev Alpha C, Z10

Re: Custom Filter field

Yes, it works perfectly for your requirement as in your first post.
--tom
Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel

Re: Custom Filter field

Sorry that post was directed at the Roshni's post. But yeah, tommy20 are you knowledgeable on how to do it like Roshni at all? I would love to see a confirmation implementation..if it's not too much to ask Smiley Very Happy.
_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Custom Filter field

I've always thought filters were best for character by character validation.  To validate an entire Field I would use something in the 'save' processing, when the field looses focus, or I would have a fieldChangedListener and revert anything to a known correct value if invalid.  But this is problematic, because if the user replaces 5 with 1, that is not valid, however they might be then adding a 5, for 15 seconds, which is valid.  So in this case I would do it in the save processing, and/or when the Field looses focus.

 

The NumericChoiceField is a good idea and you might also consider a SpinboxField.

Trusted Contributor
Posts: 204
Registered: ‎07-03-2011
My Device: Torch
My Carrier: Digicel

Re: Custom Filter field

Ok thanks very much, but I have two things to say though

I really do want it to work character by character. for example. When they enter the letter 6, the only number they can enter next is 0. And if they enter any digit higher than 7 it prevents them from entering anything else. is that possible?

And if they type for example 4(remembering that I only want entries five and higher ), it disable's continuing until they enter a valid number. Any suggestions

You see the reason I'm running away from the SpinBoxField and NumericChoiceField is because I'll have to fill all the numbers in from 5 to 60. Isn't that just ineffecient? Or is there a quick way to do that? I know above the NumericChoiceField has the ability to make it easy, but I don't want to much of my screen being given to those things. I'll give your suggestions some thought, but is there anyway to do it in the fashion that I wish?
_______________________________________________________
Never be close-fisted with the knowledge you've received. For if others were the same to you, you would not be painted as the person you are; the flowing definition of you, would be non existent.

You would be meaningless.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Custom Filter field

Ignore the coding, think of the user.  SpinBox is better for the user isn't it.

 

Regarding doing things character at a time, say they enter 7, then backspace and enter 1.  is that valid?  You might have loads of special cases like this.  Make it simple for the user, not the programmer,