Hi guys
I am completely stuck on this for a few days...any help will be super
appreciated!
I have an AutoCompleteTextView which I want to populate with artist
names based on what the user types. I have a method which makes a call
to the Musicbrainz API which takes a few seconds so I do that in a
separate thread.
The problem is that when I get the list of artist names back from
Musicbrainz, I try and update the ArrayAdapter with a Handler on the
UI thread (which is called from the Musicbrainz thread), but its only
on the next letter entered in the textview that the ArrayAdapter
changes are displayed to the user.
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 Beginners" 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-beginners?hl=en