I've posted this in a couple of places without any help so hopefully
this group has some helpful people :)
I have an AutoCompleteTextView which I want to populate with artist
names based on what the user types.
The problem is that I have a thread for retrieving the names from
a web service, but when I try and update the ArrayAdapter with a
Handler on the
UI thread (which is called from the web service thread), nothing gets
updated on screen.
When I type another letter though, then the previous results from web
service are displayed in the autocompletetextview rather than the
latest.
1) Type 'abc'
2) Web Service gets 'abc1', 'abc2' and 'abc3' and calls Handler to
notify GUI thread
3) Set 'abc1', 'abc2' and 'abc3' on adapter using adapter.add(str)
4) Call notifyDataSetChanged()
5) Nothing changes on GUI :(
6) Type another letter so textview reads 'abc1'
7) Web Service retrieves 'abc1x', 'abc1y' and 'abc1z' and calls
Handler to notify GUI thread
8) Set 'abc1x', 'abc1y' and 'abc1z' results on the adapter
9) Call notifyDataSetChanged()
10) GUI now shows 'abc1' (From step 3)
Here is my code:
public void onCreate(Bundle savedInstanceState) {
...
AutoCompleteTextView actv = (AutoCompleteTextView)
findViewById
(R.id.artist_search_text_edit);
actv.addTextChangedListener(textWatcher);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, new ArrayList<String>());
actv.setAdapter(adapter);
}
TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int
count) {
Thread t = new Thread() {
public void run() {
ArrayList<String> names =
(ArrayList<String>) getArtistStrings
(getInput());
artistNames = names;
messageHandler.sendEmptyMessage
(0);
}
};
t.start();
}
@Override
public void beforeTextChanged(CharSequence s, int
start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
private String getInput() {
EditText artistInput = (EditText) findViewById
(R.id.artist_search_text_edit);
String artistSearchString = artistInput.getText
().toString();
return artistSearchString;
}
private void updateTextAdapter(List<String> data) {
adapter.clear();
for (String artistName : data) {
adapter.add(artistName);
}
adapter.notifyDataSetChanged();
}
private Handler messageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
updateTextAdapter(artistNames);
}
};
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en