Skip to content Skip to sidebar Skip to footer

How To Use Dismissdropdown() In Simpleadapter Listview In Android?

I am building an android application where I am using AutoComplete text to show the search of google place name. Now the problem is I want to dismiss the list when I selected any

Solution 1:

Got it! Calling atvPlaces.setText() in the onItemClick triggers the PlacesTask all over again.

So you would need to call atvPlaces.removeTextChangedListener() before you call atvPlaces.setText().

And you can set the listener back after you update the text.

protectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        atvPlaces = (AutoCompleteTextView) findViewById(R.id.atv_places);
        atvPlaces.setThreshold(1);
        mTextWatcher = newMyTextWatcher();
        atvPlaces.addTextChangedListener(mTextWatcher);
}
protectedvoidonPostExecute(final List<HashMap<String, String>> result) {
            String[] from = newString[] {"description"};
            int[] to = new int[] { android.R.id.text1 };

            SimpleAdapter adapter = newSimpleAdapter(MainActivity.this, result,
                    android.R.layout.simple_list_item_1, from, to);
            atvPlaces.setAdapter(adapter);
            atvPlaces.setOnItemClickListener(newAdapterView.OnItemClickListener() {
                @OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
                    atvPlaces.removeTextChangedListener(mTextWatcher);
                    String a = search_bar.getText().toString();
                    a = a.substring(0, Math.min(a.length(), 12));
                    atvPlaces.setText(a+"...");    
                    atvPlaces.dismissDropDown();
                    atvPlaces.addTextChangedListener(mTextWatcher);
                }
            });
            atvPlaces.performValidation();
            atvPlaces.showDropDown();
        }
privateclassMyTextWatcherimplementsTextWatcher {
        @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int
        count) {
            placesTask = newPlacesTask();
            placesTask.execute(s.toString());
        }

        @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
        int after) {
            // TODO Auto-generated method stub
        }

        @OverridepublicvoidafterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    }

Solution 2:

Try using post(Runnable r) method with the AutoCompleteTextView like this:

atvPlaces.post(new Runnable() {
    publicvoidrun() {
        atvPlaces.dismissDropDown();
    }
}

Hope that Helps!1

Post a Comment for "How To Use Dismissdropdown() In Simpleadapter Listview In Android?"