Refresh ArrayAdapter dynamically - android

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()

Related

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: offline dictionary

I am developing a dictionary application which stores the database offline. Now the problem is I am using autocomplete textview to display suggestions. As I type the suggestions should be loaded. If I don't use AsyncTask the typing becomes really slow and it gives a lag. And if I use AsyncTask the, still the suggestions take too much of time to load from database. Meaning the database search query is really slow and does not display results quickly. Please help me as I am stuck on this for quite a long time. My code is as below. Thanks in advance.
autoComplete.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(final CharSequence s, int start,
int before, int count) {
new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... params) {
results = db.getWords(s.toString());//arraylist
return null;
}
protected void onPostExecute(Void result) {
adapter.addAll(results);
}
}.execute();
}
#Override
public void beforeTextChanged(final CharSequence s, int start,
int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
query.showDropDown();
}
I am doing everything in onTextChanged(). Here is my sqlite query for searching the results."SELECT WORD FROM tblWords WHERE Word LIKE '%s%%' GROUP BY Word LIMIT 5". Note that the database is extremely large. Thanks.
I notice two things:
You have no minimum length to start searching. I would think that normally it doesn't make a lot of sense to start searching at 1 or 2 characters, partly because you would get way too many results. It's also possible it starts searching at 0 characters, so perhaps it's trying to add your complete database to the adapter.
Another thing I notice is that you never clear the collection of the adapter, but only add everything. This will also bloat the list with unnecessary results.
A last note is style-related: I wouldn't use the addAll method, but instead update the list used by the Adapter and then call notifyDataSetChanged() on the adapter. I think this is just style, but perhaps it would also increase performance.

Filter ListView in real time

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.

quick search within app in android?

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());
};
};

show data in listview from webservice according to keypress in android

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.

Categories

Resources