我在我的应用程序中使用 AutoCompleteTextView,但它意外地工作。自动完成列表显示列表中不需要的文本,但当我选择此文本并从自动完成文本视图中移除焦点时,它会将正确的文本设置为自动完成文本字段。另一个问题是我想在 autocompletetext 字段中获取所选文本的 id,当我在 autocompletetextview 的自定义适配器中填充列表时,我正在关联这个 id。这是我正在使用的代码。
autoComMarker = (AutoCompleteTextView) findViewById(R.id.auto_rainfall_of_markaz_id);
autoComMarker.setThreshold(1);
CustomMarkazAdapter adapter = new CustomMarkazAdapter(getBaseContext(), R.layout.custom_auto_com_listview, marqazList);
autoComMarker.setAdapter(adapter);
autoComMarker.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
autoComMarker.setTag(view.getTag());
Log.e("Markaz list", ""+view.getTag());
}
});
这是 CustomMarkazAdapter 的代码:
public class CustomMarkazAdapter extends ArrayAdapter<Markaz>{
private ArrayList<Markaz> items;
private Context CurrentContext;
private Markaz CurrentItem;
public CustomMarkazAdapter(Context context, int textViewResourceId,
ArrayList<Markaz> objects) {
super(context, textViewResourceId, objects);
items = objects;
CurrentContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if ((items == null) || ((position + 1) > items.size()))
return convertView; //Can't extract item
CurrentItem = (Markaz)items.get(position);
LayoutInflater vi = (LayoutInflater)CurrentContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.custom_auto_com_listview, null);
TextView text = (TextView) convertView.findViewById(R.id.custom_auto_com_text);
text.setText(CurrentItem.getMarkazName());
convertView.setTag(CurrentItem.getMarkazID());
Log.e(CurrentItem.getMarkazName(),""+CurrentItem.getMarkazID());
return convertView;
}
}
我在这里设置的markazID 不是我正在寻找的,它根据autocompletelistview 项目位置设置markaz id。我不需要我需要与markazList 一起传递的markaz id。请帮助我,非常感谢任何帮助。