How to display the value of a HashMap key when user clicks autocom

1.5k views Asked by At

I'm new to android development and Java programming. In my code (shown below), when the user clicks the search button, the value of a key is displayed in displayField TextView. But I want it so that the value is displayed when the user clicks (touches) an item on the AutoCompleteTextView dropdown items, without needing to click the 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);

    // Get a reference to the AutoCompleteTextView in the layout
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_searchField);

    // Gets the string array
    String[] music_note = getResources().getStringArray(R.array.music_notes_array);

    // Creates the adapter and set it to the 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); // I want to completely remove this button eventually
    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, a female deer");
    map.put("Ray", "a drop of golden sun");
}
2

There are 2 answers

5
Lennon Spirlandelli On BEST ANSWER

I didn't understand why you needed autocomplete_searchField in your code, anyway, if I understood well you just need to remove button.setOnClickListener and add the 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()));

    }

});
1
Krishna V On

You need to set ItemClickedListener to AutoCompleteTextView.

textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View arg1, int position, long arg3) {
        String note = autocomplete_searchField.getText().toString();
        displayField.setText(map.get(note));
    }
});