javaandroiddropdownmaterial-design-3

Getting the selected item from the dropdown does not work


I am using Material Dropdown in my android application. Everything works until I want to get the data from the dropdown. I want to get the data from the dropdown when the save button is clicked. I do the design according to the Material Design documentation, but it doesn't say how to get the data. I used app:simpleItems to show the items in the dropdown:

<AutoCompleteTextView
    android:id="@+id/storeDropdown"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="none"
    app:simpleItems="@array/stores" />

However, I don't know how to get the selected item. I tried this:

storeDropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        store = parent.getItemAtPosition(position).toString();
    }

    @Override
        public void onNothingSelected(AdapterView<?> parent) {
    }
});

But I get null. I also tried setting the ArrayAdapter manually but the result was the same and the appearance is set to a plain Spinner. I wanted to use something like the getSelectedItem() method, but that only works with the Spinner. How do I get the selected item from the dropdown?


Solution

  • So I assume that you want to get the selected Item when you click a button. As this is an AutoCompleteTextView it has the same property of Textviewor say EditText. So you can get the selected item by simply doing this.

     String selectedItem;
    
     button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
          selectedItem = autoCompleteTextview.getText().toString();
        }
    });