I have an AutoCompleteTextView in my layout and I want to do an API call which takes the first character entered as a parameter. I do this API call in an AsyncTask to which I pass the first character as a parameter.
What listener should I use on AutoCompleteTextView so that AsyncTask call happens right after the first character is inputted ?
Try this link
http://makovkastar.github.io/blog/2014/04/12/android-autocompletetextview-with-suggestions-from-a-web-service/
I achieved the above functionality using shared article.
Fixed this issue using addTextChangedListener.
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().trim().length() == 1) {
isFileReceived = false;
mAirport = new AsyncTaskAirport(s.toString());
mAirport.execute((Void) null);
}
Related
I have a registration screen with large number of registration fields, and when user click register button, I pass field values to presenter. In presenter I validate these values and create an object. The problem is a large number of arguments in register() method. I think that I should avoid this situation, but I have no idea how to do it.
Maybe you could explore the Builder pattern.
It allows to keep the code clean when you need to pass a big number of arguments.
It's also very useful when you don't know the exact number of arguments that will be passed, because some of them might not be mandatory.
In practice, you would have something like
MyObject myObject
void register() {
myObject = MyObject.Builder(<mandatory arguments>)
.argument1(<argument 1>)
.argument2(<argument 2>)
...
.create();
if (myObject == null) fail();
else dosomething();
}
One way I have done this previously is to use a TextWatcher on each field that has to be completed:
myEditText.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) {
presenter.myEditTextChanged(s.toString());
}
});
Then have the corresponding methods in the presenter update your entity. This way when the user finally clicks register all the details will already be waiting in your presenter.
It also has the advantage that you can do validation as the user progresses - ie the register button isn't enabled until all fields are valid.
If you are using ButterKnife, RxBinding or DataBinding the code is more succinct as well.
I want to filter a ListView in real time. I have an EditText in the ActionBar, and each time the user writes a character I want to update the cursor of the ListView filtering the information.
I have done an AsyncTask to perform the query but I have two questions:
1º) If the user types three characters I'm creating three AsyncTasks (one to search the first character, one to search the two first characters, and finally one to search the three characters). Is there a simple way to say to the AsyncTask to replace the previous task with the new one?
2º) How can I put a small delay to start the AsyncTask? So if the user types three characters without stopping, I will not create the AsyncTask until the end.
Thanks!
Hmmm...Not totally sure why your using AsyncTask for search in real-time. Here's how I would(and have successfully) do it.
Add a TextChangeListener to your Edittext:
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
searchResults = myDbHelper.searchAll(s.toString());
searchAdapter.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
I am searching through a sqlite database everytime a user presses a key. My searchResults is used to populate the ListView, and so after I set the get the search results, tell the list adapter that the data set changed.
Hope this helps.
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
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)"
I am developing an android app for a Korean client. I have to search a data according to key press, like, if I press A then all the data who started from A show in listview where data come from a web service in listview. Is it possible?
If yes, then how to do it. If possible give me some code or link which I can use?
Thanks.
Yes its possible.You can add TextChangedListener on your edittext and in the onTextChanged event get the date from the webserver as json or xml and parse that data and show the data in a listview.
edittext.addTextChangedListener(textWacther);
final TextWatcher textWacther = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
getdata();
}
};
Getting the data from webserver this question might be helpful for you
How to connect to a Remote Database with Webservices?
At the area where getData() is called in #Syam's code, fetch the new data according to the text entered in the edit text, put it in the array that has the source of ListView, don't forget to call setDataSetChanged() on the adapter in the end.