Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onItemClick setting text to the AutoCompleteTextView

Tags:

android

I have added a onTextChangedListener to my autocomplete textview and populate it using an async task

mAutoComplete.addTextChangedListener(new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        //run an async tast to get autocompletes
    }
    @Override
    public void afterTextChanged(Editable s) {
    }
});

private class getAutoCompletes extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        //get autocompletes
    }
    @Override
    protected void onPostExecute(String result) {
        //create an adapter
        mAdapter AutoCompleteAdapter = new mAdapter(
                mActivity.this,
                R.layout.m_layout,
                R.id.m_id, autocompletesList);
        //set it to the autocomplete textview
        mAutoComplete.setAdapter(AutoCompleteAdapter);
        //show the dropdown
        mAutoComplete.showDropDown();
    }
}

Then I have setOnItemClickListener(new AdapterView.OnItemClickListener() {} on the mAutoComplete. But doing nothing in it.

Still I get the String representation of the adapter as text in the mAutoComplete, when I click on any item in the dropdown

com.xxxx.app.mAdapter@4342ca0

No where I am setting the text for the mAutoComplete.

EDIT:

Adapter Class:

public class mAdapter extends ArrayAdapter<customDS> {

    private LayoutInflater mInflater = null;
    private Context ctx;
    public ArrayList<customDS> values = new ArrayList<customDS>();

    public mAdapter(Context context, int resource,
            int textViewResourceId, ArrayList<customDS> objects) {
        super(context, resource, textViewResourceId, objects);
        values = objects;
        ctx = context;
        mInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return values.size();
    }

    public customDS getItem(int position) {
        return values.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
        public TextView title;
        public TextView description;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.m_layout,
                    parent, false);
            holder.title = (TextView) convertView
                    .findViewById(R.id.m_id);
            holder.description = (TextView) convertView
                    .findViewById(R.id.m_id2);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(values.get(position).title);
        holder.description.setText(values.get(position).description);

        return convertView;
    }
}
like image 777
Archie.bpgc Avatar asked Nov 06 '13 06:11

Archie.bpgc


3 Answers

You can create a subclass of AutoCompleteTextView and override the "replaceText" method, as in it's superclass (AutoCompleteTextView) "replaceText" is used to replace the current text in the view when a result is clicked.

public class CustomAutoCompleteTextView extends AutoCompleteTextView {
    public CustomAutoCompleteTextView(Context context) {
        super(context);
    }

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void replaceText(CharSequence text) {
        // do nothing so that the text stays the same
    }
}
like image 68
jaredpetker Avatar answered Nov 02 '22 23:11

jaredpetker


Still I get the String representation of the adapter as text in the mAutoComplete, when I click on any item in the dropdown

That's because when you select an item from drop down the auto complete widget will call the toString() method to fill the EditText where the input is inserted.

Try overriding the toString() method of the customDS class to return what you want to see there from the object.

like image 44
user Avatar answered Nov 03 '22 00:11

user


This is more simple. Try to add this in your activity. It works for me.

mAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        mAutoComplete.setText(((TextView) view).getText());

        // Or maybe you need to do something like this (it depends from your R.layout.m_layout):
        // LinearLayout l = (LinearLayout) view;
        // TextView t = (TextView) l.getChildAt(0);
        // mAutoComplete.setText(t.getText());
    }
});
like image 31
MangekyōSharingan Avatar answered Nov 03 '22 01:11

MangekyōSharingan