I have a application already working fine. I need to implement a quick search feature to it. Quick search i mean as the users types in every single character i want to result to be fetched. My Activity is a list activity for which the data is coming from the database query. Say the list view has 50 items, and when the user searches with word as "test" I want to query the database and filter the items and display it in the same list view. Something like the contact search in android. Please let me know how to implement this. A quick sample would be help full. Thank you.
you can do by this
I implemented this feature in one of my previous app. The entire code is too long to be pasted here. So, I have posted some portions of it. You may read a bit about the approach used by me and get it done for your app.
EditText filterText;
words = new MySimpleCursorAdapter(Main.this, R.layout.emptylist, temp, from, to);
filterText= (EditText) findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
words.setFilterQueryProvider(new FilterQueryProvider() {
#Override
public Cursor runQuery(CharSequence constraint) {
Cursor cur=mDbHelper.fetchSugNotes(filterText.getText().toString());
return cur;
}
});
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(android.text.Editable s) {
};
public void beforeTextChanged(CharSequence s, int start, int count, int after) {};
public void onTextChanged(CharSequence s, int start, int before, int count) {
words.getFilter().filter(s.toString());
};
};
Related
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. :)
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.
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.
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.
I have an Activity with an AutoComplete box on it. Whenever the text changes, I want to call a web service and populate the ArrayAdapter with the new String[] returned. This part all works great, except the list in the UI isn't being refreshed when there are all new values in String[] schools. The original list populated in my onCreate always remains.
I read somewhere I needed to update it on the same thread the UI is running on, so I tried the Runnable listed in my code below. But that, as well as just updating my class variable schools does not work alongside notifyOnDataSetChange()
Where am I going wrong?
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
schools = SchoolProxy.search("Co");
autoCompleteAdapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line, schools);
autoComplete = (AutoCompleteTextView) findViewById(R.id.edit);
autoComplete.addTextChangedListener(textChecker);
autoComplete.setAdapter(autoCompleteAdapter);
}
...
....
...
final TextWatcher textChecker = 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)
{
schools = SchoolProxy.search(s.toString());
runOnUiThread(updateAdapter);
}
};
private Runnable updateAdapter = new Runnable() {
public void run() {
autoCompleteAdapter.notifyDataSetChanged();
}
};
As a less important side note, if each item in schools has a name \n city,state. Is it possible to have the city & state on a second line within the auto drop down box? Just for formatting purposes. Would look cleaner if I could.
Thank you!
Have you tried the setNotifyOnChange(true)? It is supposed to properly do what you are doing manually whenever you use methods that change the list (add(T), insert(T, int), remove(T), clear()). Perhaps you have to modify the array through these methods on the ArrayAdapter?
I'm not sure if the ArrayAdapter is actually holding the reference to schools or just copied the contents while constructing the ArrayAdapter. Maybe you can take a look at the AOSP code to see what they're doing in that constructor.
After you dynamically change adapter, just need to call:
AutoCompleteTextView.showDropdown()