java - How to display the value of a HashMap key when user clicks autocom -
i'm new android
development , java
programming. in code (shown below), when user clicks
search
button
, value
of key
displayed in displayfield textview
. want value
displayed when user clicks (touches) item on autocompletetextview
dropdown items, without needing click
search
button
.
import java.util.hashmap; import java.util.map; import android.content.context; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.view; import android.view.inputmethod.inputmethodmanager; import android.widget.arrayadapter; import android.widget.autocompletetextview; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class mainactivity extends actionbaractivity { map<string, string> map = new hashmap<string, string>(); private edittext autocomplete_searchfield; private textview displayfield; string note; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); autocomplete_searchfield = (autocompletetextview) findviewbyid(r.id.autocomplete_searchfield); // reference autocompletetextview in layout autocompletetextview textview = (autocompletetextview) findviewbyid(r.id.autocomplete_searchfield); // gets string array string[] music_note = getresources().getstringarray(r.array.music_notes_array); // creates adapter , set autocompletetextview arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, musicnote); textview.setadapter(adapter); displayfield = (textview) findviewbyid(r.id.displayfield); button button = (button)findviewbyid(r.id.button); // want remove button button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string note = autocomplete_searchfield.gettext().tostring(); displayfield.settext(map.get(note)); map.put("doe", "a deer, female deer"); map.put("ray", "a drop of golden sun"); }
i didn't understand why needed autocomplete_searchfield
in code, anyway, if understood need remove button.setonclicklistener
, add following code:
textview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int position, long id) { displayfield.settext(map.get(adapterview.getitematposition(position).tostring())); } });
Comments
Post a Comment