我在我的自动完成文本视图中添加了一个 onTextChangedListener 并使用异步任务填充它
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();
}
}
然后我setOnItemClickListener(new AdapterView.OnItemClickListener() {}
在 mAutoComplete 上。但在其中什么也不做。
当我单击下拉列表中的任何项目时,我仍然在 mAutoComplete 中将适配器的字符串表示形式作为文本
com.xxxx.app.mAdapter@4342ca0
没有我在哪里设置 mAutoComplete 的文本。
编辑:
适配器类:
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;
}
}