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;
}
}
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
}
}
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.
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());
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With