Format phone number on blackberry [closed] - blackberry

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I need to enter a mobile number in a edit field. I need to display the number in this format:
123-456-7890
For example: 1234567890 is my mobile number, while am entering this into the text field,
after the first 3 digits I need to place a '-', after the next 3 digits, I need to place another '-'.
If I enter 123 then automatically place '-' in text field, after 456 place another '-' ,no need of placing for the last 4 digits.
Seems similar to displaying text in currency format, but when getting text from that text field I need to get the actual mobile number without any '-' ("1234567890", not 123-456-7890)
Any ideas on how to do this?

Please try to add Listener on EditFiled and check length of entered phoneno ans set "-" at 3 and 7 position see following code may be help full.
editField.getEditField().setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
EditField sfield = (EditField)field;
String number = sfield.getText();
if (number.length() == 3 || number.length() == 7) {
setFormat(number);
}
}
});
add(editField);
Create follwoing method on mainscreen class
private void setFormat(String newPhoneNo){
editField.setText(newPhoneNo+"-");
}
Whenever you gettext form edit remove "-".

Please check this code,
public MyScreen()
{
setTitle("MyTitle");
final EditField ef = new EditField();
add(ef);
ef.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
if (ef.getText().length() == 4 && !ef.getText().endsWith("-"))
{
ef.setText(""+ef.getText().substring(0,3)+"-"+ef.getText().charAt(3));
}
if (ef.getText().length() == 8 && !ef.getText().endsWith("-"))
{
ef.setText(""+ef.getText().substring(0,7)+"-"+ef.getText().charAt(7));
}
}
});
}

Try out by trying first the native TextField.PHONENUMBER constraint.
public void startApp()
{
mDisplay = Display.getDisplay(this);
mTextField = new TextField("Text Field Label:",null,MAXCHARS,TextField.PHONENUMBER);
mForm = new Form("MIDlet Developer Guide: Display a text field.");
mForm.append(mTextField);
mDisplay.setCurrent(mForm);
}

Related

how to accept email or phone number in the single edit text

I have only one edit text field to accept email or phone number. how to change the input type based on the 1st character ? based on user input i need to perform different operations.. The main thing is i should identify whether that is email or phone number. how to do this?
In your, EditText set a TextWatcher which calls a function to check if the text is email or is a phone number.
Get the text from your TextView like :
String text = textView.getText().toString();
Adding the listener to your TextView :
textView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(....){}
#Override
public void beforeTextChanged(...){}
#Override
public void afterTextChanged(Editable s) {
if(isEmail(text)){//do your stuff}
if(isPhone(text)){//do your stuff}
}
}
Your methods would look something like this:
public static boolean isEmail(String text) {
String expression = "^[\\w\\.-]+#([\\w\\-]+\\.)+[A-Z]{2,4}$";
Pattern p = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(text);
return m.matches();
}
public static boolean isPhone(String text) {
if(!TextUtils.isEmpty(text)){
return TextUtils.isDigitsOnly(text);
} else{
return false;
}
}
This might not be possible with just checking the first digit. Though you can put validations inside onTextChanged method.
Novo Lucas's answer is correct.
My Views :
First thing is that you can't know input type using the first letter cause some email addresses begin with numbers.
On button click you can identify type by knowing if the text contains only numbers or alphanumeric characters. For more info about code you can search it on Google. I will try to add code after some time. Good Luck

How to add text to the textfield inside dataChangedListener in codename one?

I am using TextField and on addDataChangedListener I am trying to add some character to the text field if length of the text field is 2 but it is not working for me.can you please help me how to achieve my requirement.
For this thing i use below mention code,
text.addDataChangedListener(new DataChangedListener() {
#Override
public void dataChanged(int type, int index) {
if(text.getText().length() == 2)
text.setText(text.getText() + "/");
}
});
Instead of DataChangedListener use ActionListener which got triggered when the TextField lost focus.
You can't modify the content of a TextField while typing because of its Async state.
text.addActionListener(evt -> {
if(text.getText().length() == 2)
text.setText(text.getText() + "/");
}
});

java.lang.IllegalStateException: trying to requery an already closed cursor android.database.sqlite.SQLiteCursor#

I've read several related posts and even posted and answer here but it seems like I was not able to solve the problem.
I have 3 Activities:
Act1 (main)
Act2
Act3
When going back and forth Act1->Act2 and Act2->Act1 I get no issues
When going Act2->Act3 I get no issues
When going Act3->Act2 I get occasional crashes with the following error: java.lang.IllegalStateException: trying to requery an already closed cursor android.database.sqlite.SQLiteCursor#.... This is a ListView cursor.
What I tried:
1. Adding stopManagingCursor(currentCursor);to the onPause() of Act2 so I stop managing the cursor when leaving Act2 to Act3
protected void onPause()
{
Log.i(getClass().getName() + ".onPause", "Hi!");
super.onPause();
saveState();
//Make sure you get rid of the cursor when leaving to another Activity
//Prevents: ...Unable to resume activity... trying to requery an already closed cursor
Cursor currentCursor = ((SimpleCursorAdapter)getListAdapter()).getCursor();
stopManagingCursor(currentCursor);
}
When returning back from Act3 to Act2 I do the following:
private void populateCompetitorsListView()
{
ListAdapter currentListAdapter = getListAdapter();
Cursor currentCursor = null;
Cursor tournamentStocksCursor = null;
if(currentListAdapter != null)
{
currentCursor = ((SimpleCursorAdapter)currentListAdapter).getCursor();
if(currentCursor != null)
{
//might be redundant, not sure
stopManagingCursor(currentCursor);
// Get all of the stocks from the database and create the item list
tournamentStocksCursor = mDbHelper.retrieveTrounamentStocks(mTournamentRowId);
((SimpleCursorAdapter)currentListAdapter).changeCursor(tournamentStocksCursor);
}
else
{
tournamentStocksCursor = mDbHelper.retrieveTrounamentStocks(mTournamentRowId);
}
}
else
{
tournamentStocksCursor = mDbHelper.retrieveTrounamentStocks(mTournamentRowId);
}
startManagingCursor(tournamentStocksCursor);
//Create an array to specify the fields we want to display in the list (only name)
String[] from = new String[] {StournamentConstants.TblStocks.COLUMN_NAME, StournamentConstants.TblTournamentsStocks.COLUMN_SCORE};
// and an array of the fields we want to bind those fields to (in this case just name)
int[] to = new int[]{R.id.competitor_name, R.id.competitor_score};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter tournamentStocks = new SimpleCursorAdapter(this, R.layout.competitor_row, tournamentStocksCursor, from, to);
//tournamentStocks.convertToString(tournamentStocksCursor);
setListAdapter(tournamentStocks);
}
So I make sure I invalidate the cursor and use a different one. I found out that when I go Act3->Act2 the system will sometimes use the same cursor for the List View and sometimes it will have a different one.
This is hard to debug and I was never able to catch a crashing system while debugging. I suspect this has to do with the time it takes to debug (long) and the time it takes to run the app (much shorter, no pause due to breakpoints).
In Act2 I use the following Intent and expect no result:
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(this, ActivityCompetitorDetails.class);
intent.putExtra(StournamentConstants.App.competitorId, id);
intent.putExtra(StournamentConstants.App.tournamentId, mTournamentRowId);
startActivity(intent);
}
Moving Act1->Act2 Act2->Act1 never gives me trouble. There I use startActivityForResult(intent, ACTIVITY_EDIT); and I am not sure - could this be the source of my trouble?
I would be grateful if anyone could shed some light on this subject. I am interested in learning some more about this subject.
Thanks,D.
I call this a 2 dimensional problem: two things were responsible for this crash:
1. I used startManagingCursor(mItemCursor); where I shouldn't have.
2. I forgot to initCursorAdapter() (for autocomplete) on onResume()
//#SuppressWarnings("deprecation")
private void initCursorAdapter()
{
mItemCursor = mDbHelper.getCompetitorsCursor("");
startManagingCursor(mItemCursor); //<= this is bad!
mCursorAdapter = new CompetitorAdapter(getApplicationContext(), mItemCursor);
initItemFilter();
}
Now it seems to work fine. I hope so...
Put this it may work for you:
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
orderCursor.requery();
}
This also works
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
startManagingCursor(Cursor);
}

smartgwt listgrid set cursor to hand over an icon field

I've been working on this problem for quite a while but have not been able to solve it.
I have a listgrid with a field type icon. I would like to change the cursor to "hand" over the icon.
I've been searching the web and saw that a couple of solutions existed.
One of them is using addCellOverHandler for the list grid. But I don't understand how you can change the cursor for the specified field of the listgrid.
this.addCellOverHandler(new CellOverHandler() {
#Override
public void onCellOver(CellOverEvent event) {
// not able to get the field and setCursor()
}
});
My field in the listgrid is defined as:
ListGridField iconField = new ListGridField("icon");
iconField.setAlign(Alignment.CENTER);
iconField.setType(ListGridFieldType.ICON);
iconField.setIcon("icons/icon.gif");
Like someone pointed out on the forum, a setCursor() method exist for the listgrid, but not for the field only...
If anybody has a clue...
Thanks
After some more (a lot more...) googling, I found this:
http://forums.smartclient.com/showthread.php?t=15748
The thing is to Override the getCellStyle method in the listgrid.
Here is the code I use:
#Override
protected String getCellStyle(ListGridRecord record, int rowNum, int colNum) {
if (colNum==6){
return "EC_pointer";
}
return super.getCellStyle(record, rowNum, colNum);
}
and in my CSS file:
.EC_pointer {
cursor: pointer;
}
The major fallout is that you have to know in advance the column number of the field.
Further to my comment and adding information from here I tested the following code which works with SmartGwt2.4 under Firefox 5.0.
demandesGrid.setCanHover(true);
demandesGrid.setShowHover(false);
demandesGrid.addCellHoverHandler(new CellHoverHandler() {
#Override
public void onCellHover(CellHoverEvent event) {
if (event.getColNum() == demandesGrid.getFieldNum("icon")) {
// SC.say(demandesGrid.getChildren()[3].toString());
demandesGrid.getChildren()[3].setCursor(Cursor.POINTER);
} else {
demandesGrid.getChildren()[3].setCursor(Cursor.DEFAULT);
}
}
});
I don't know if the index of the ListGridBody is constant; I found it with the SC.say line.
How about
grid.addCellOverHandler(new CellOverHandler() {
#Override
public void onCellOver(CellOverEvent event) {
//cellOver event to get field and refresh the cell
//grid.refreshCell(i, j);
}
});
The best approach is fully demonstrated here (take a look at how "comments/stats" field is being initialized).
In short, u have to extend ListGrid and override createRecordComponent method. In this method you can make any custom component you like and it will be show in grid cell.
Also ListGrid should be initialized with:
listGrid.setShowRecordComponents(true);
listGrid.setShowRecordComponentsByCell(true);

How to get input text into the listfield of autocompletefield in blackberry?

I want to make something like the contact selection in OS 5.0 while composing message
For that I have made an autocompletefield with contacts as datasource. Now I want to add the user input, that is what he types into the drop down list which appears in autocompletefield so that if the contact is not available in the phonebook he can use the number.
Check this link: How to get the selected item as String in a Blackberry AutoCompleteField?
public void onSelect(Object selection, int type) {
super.onSelect(selection, type);
if(selection != null) {
String selectionAsString = getEditField().getText();
// Do whatever else you need to do with the String.
}
}

Resources