Question

I am using autocompleteTextview for trying get some data from server.

private void initAutoCompleteTextViews() {
        mAutoCompleteList = new ArrayList<String>();

        mStreet.setThreshold(3);

        mStreet.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 3){
                    getSuggestion();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

My problem is when I choose one of suggestion and the getSuggestion() calls again because the lengh also greater than 3.

Is there any solution that I can choose suggestion and just set it on autocompleteTextview?

Was it helpful?

Solution

I think it can help you

private AutoCompleteTextView mSearchbar;
private ArrayAdapter<String> mAutoCompleteAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    mAutoCompleteAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
    mSearchbar = (AutoCompleteTextView) findViewById(R.id.searchbar);
    mSearchbar.setThreshold(3);
    mSearchbar.setAdapter(mAutoCompleteAdapter);
    mSearchbar.addTextChangedListener(new TextWatcher() {

        private boolean shouldAutoComplete = true;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            shouldAutoComplete = true;
            for (int position = 0; position < mAutoCompleteAdapter.getCount(); position++) {
                if (mAutoCompleteAdapter.getItem(position).equalsIgnoreCase(s.toString())) {
                    shouldAutoComplete = false;
                    break;
                }
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (shouldAutoComplete) {
                new DoAutoCompleteSearch().execute(s.toString());
            }
        }
    }
}

private class DoAutoCompleteSearch extends AsyncTask<String, Void, ArrayList<String>> {
    @Override
    protected ArrayList<String> doInBackground(String... params) {
        ArrayList<String> autoComplete = new ArrayList<String>();
        //do autocomplete search and stuff.
        return autoComplete;
    }

    @Override
    protected void onPostExecute(ArrayList<String> result) {
        mAutoCompleteAdapter.clear();
        for (String s : result)
            mAutoCompleteAdapter.add(s);
    }
}

OTHER TIPS

Implement

mStreet.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos,
            long id) {
          Toast.makeText(mContext,"new item selected.", Toast.LENGTH_LONG).show();

   // your code when item is selected from suggestion
    }
});

instead of implementing addTextChangedListene(new TextWatcher).

in your add textChanged Listener,,add extra condition in if loop like this

@Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 3 && mStreet.getText().length() != ResultString.length()){
                    getSuggestion();
                }

In ur getSuggestion() method use a string like this,,

public void getSuggestion(){

.......
......

ResultString = (Your Server Response);
}

There is a method isSelected() .Try to use it.

Like actv.isSelected();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top