Removing TextChangedListener then re-adding it - android

So I've been trying to implement the TextWatcher for Android and ran into a few problems with the TextChangedListener being called multiple times or going into an infinite loop as I want to convert the text in the EditText widget into a currency formatted string.
What I did to work around this was create my own custom TextWatcher and then in the afterTextChanged event did something like the following
public class CurrencyTextWatcher implements TextWatcher {
private EditText et;
public CurrencyTextWatcher(EditText editText) {
et = editText;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);
et.setText(myCurrencyString);
et.addTextChangedListener(this);
}
}
So my question is, is there a better way of doing this? I want to have the one EditText Widget to hold where the edits go and the resulting formatted string.
Also is there actually any other issues that comes about removing and then adding a TextChangedListener like this?
Thanks in advance

Everytime you will update (by eg calling set text) your editText the afterTextChanged will be called, so I think you should refrain from calling setText every time you are in afterTextChanged and only call it when something is really changing.
sth like this
if ( !myCurrencyString.equals(et.getText()))
{
et.setText(myCurrencyString);
}

How about following.
private void resetAddTagField() {
if (edtView != null && textWatcherListener != null) {
edtView.removeTextChangedListener(textWatcherListener);
edtView.setText(DEFAULT_TEXT);
edtView.addTextChangedListener(textWatcherListener);
}
}
What I learn: Do not underestimate power of TextWatcher :D :D

Related

Handle Change of TextView according to EditText with Empty value

I am using TextChangedListener on a EditText called "deviseValue" and make operations with it to show other values in sellValue and buyValue which are two TextViews, as follows :
deviseValue.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void afterTextChanged(Editable s) {
Double dDeviseValue = Double.valueOf(deviseValue.getText().toString());
Double sellResult = dDeviseValue*dSellValue;
Double buyResult = dDeviseValue*dBuyValue;
sellValue.setText(sellResult.toString());
buyValue.setText(buyResult.toString());
}
});
Everything works fine, BUT, when I remove all value of deviseValue (i.e : EMPTY) .. my app crashes !!
How to handle this situation so when user remove all value, deviseValue become automatically > 1. ?
App crashed because you try to convert a string value from a edittext to the double value. But when you cleared your edittext deviseValue.getText().toString() is ""(empty). That's why you got NumberFormatException.
Try to check deviseValue.getText().toString() before convert it to the double.
For example:
String dDeviseText = etEmail.getText().toString();
Double dDeviseValue = Double.valueOf(dDeviseText.isEmpty() ? "1" : dDeviseText);
And you should prevent input not a numbers characters for this edittext.

Update a specific row in a Room Database of Android

I am new to Android and need some help.
I have a listView with some edittext fields and buttons and store the information of the edittext in a room database.
Looks like this: https://imgur.com/a/NTpMUmY
Now, when I change the input of field A to, let's say AAAA, and click on the "button", the room database has to update the row.
My question is when exactly do I call the update command, and how does room know which row has changed? I don't want to update to whole table.
Let me know if I should explain it in another way.
Ok, I found a solution which works for me.
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
Dao_db.update(s.toString());
}
};
viewHolder.bearbeiten_et.addTextChangedListener(textWatcher);
I use this Textwatcher (didn't know that something like this exist) and update my db when the text changed. :)

android addTextChangedListener not fully deleting the text in Firebase when backspace

I was facing the problem with the addTextChangedListener that was not fully delete the text in Firebase. I state an example, I get my name from Firebase and setText() on the myname. So on myname editText has show my name there. When I wanted to edit, I click on editText, so I can remove the word by backspace, but when I backspace too fast, the text in editText was fully removed (client side) and there are some word did not delete on Firebase(server side).
Which mean the removing text value are not consistent and accurate with the client side and server side. The ordinary text of my name is Tommy, so I backspace until the Tommy word gone, so that was cleared but in Firebase it still showing the first character 'T' of my name.
But when I was clearing all word on the editText, I input new word such as "Hello", the Firebase will store Hello.
Code:
//delcare myname
private MultiAutoCompleteTextView myname;
myname.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(!myname.getText().toString().trim().isEmpty()){
DatabaseReference nameRef = FirebaseDatabase.getInstance().getReference()
.child(Config.URL_USER);
nameRef.child(uid).child("myName").setValue(userET.getText().toString());
parentActivity.updateHeaderUserName(myname.getText().toString());
}
}
});
This loops work until there is a single charcter in the edittext, so add an else loop and inside the else loops set the value as null

where condition in SQLite and EditText related Event

Respected Users,
I am new with android and SQLite technology.
I am getting error in following statement.
Kindly correct me.
Cursor mCur=
db.query(DATABASE_TABLE,
new String[]{ROWID,MEDICINE,KEY_DESC,PRICE},
MEDICINE+" = "+medName,null,null,null,null);
My Other question is that is there any event associated with EditText so that I can use its functionality as I was using it with text_change in C# .NET.
[When I types the text event should get fired].
For this I have tried with following events:
setOnEditActionListener and
setOnTouchListener
1) In your SQL query I assume your medName was the problem because it is a String, use:
Cursor mCur=
db.query(DATABASE_TABLE,
new String[]{ROWID,MEDICINE,KEY_DESC,PRICE},
MEDICINE+" = ?",new String[] {medName},null,null,null);
This approach also protects you from SQL injection attacks.
2) To listen for changes in an EditText, use a TextWatcher.
EditText edit = (EditText) findViewById(R.id.edit);
edit.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
}
});
For your Sqlite question please post the Logcat logs.
Add a TextWatcher to your EditText to implement 3 callback methods.

Validating edittext in Android

I'm new to android & I'm trying to write an application for a project.
I need to check whether the user has entered 7 numbers followed by one alphabet in edittext.
Example: 0000000x
How should I do that? TIA! :)
Probably the best approach would be to use a TextWatcher passed into the addTextChangedListener() method of the EditText. Here is an example use:
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable e) {
String textFromEditView = e.toString();
validateText(textFromEditView);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//nothing needed here...
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//nothing needed here...
}
});
I will leave the implementation of the validateText(String) method as an exercise for the reader, but I imagine it should be easy enough. I would either use:
A simple Regular Expression.
Or since this case is easy enough, checking that the length of the string is 8, and reviewing each character. There is a simple utility class to inspect the characteristics of characters. Character.isDigit(char) and Character.isLetter(char)
OnKeyListener listens to every key stroke in the view. you can use that to check whether the user has entered what he is supposed.
eg : if the no of char entered is 7 then
check if it follows the reqd expression format.
There is a Class called Pattern in Android in that you can give Regular Expression to match your Requirements try this follwoing code i think it may work
Pattern p = Pattern.compile( "{7}" );
Matcher m = p.matcher(String.valueOf(edittext));
This will be true only if 7 characters are there in the Text box and then you can use some menthods like "Character.isDigit(char) and Character.isLetter(char)"

Categories

Resources