3

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");
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Emzor
  • 1,380
  • 17
  • 28

2 Answers2

3

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

    }

});
Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51
  • Worked a treat! I'm grateful. – Emzor Jun 10 '15 at 15:19
  • I'm glad for helping you :) – Lennon Spirlandelli Jun 10 '15 at 15:36
  • The only problem now is that the clicked drop-down item does not respond on the first click (this only happens once, when the app is launched), I have to re-type the word and click the item before it displays the value. Any suggestions as to how to resolve this? Most of the solutions I've seen treat buttons, but not AutoCompleteTextView drop-down items. – Emzor Jun 10 '15 at 16:10
  • Sorry but I did't understand what you mean. Can you show me your code or an image from the issue? – Lennon Spirlandelli Jun 10 '15 at 19:26
  • I've had to create it as a different question since its not exactly related to the topic of this particular question. Please find question in the following link and assist. http://stackoverflow.com/questions/30763879/clicked-drop-down-item-in-autocompletetextview-does-not-respond-on-the-first-cli – Emzor Jun 11 '15 at 12:20
1

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));
    }
});
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
Krishna V
  • 1,801
  • 1
  • 14
  • 16
  • Can I replace the button.setOnClickListener(new View.OnClickListener() method with setOnItemClickListener(new AdapterView.OnItemClickListener()method? – Emzor Jun 10 '15 at 13:55