我在我的应用程序中使用AutoCompleteTextView但它意外地工作。自动完成列表显示我不想要的列表中不需要的文本,但是当我选择此文本并从autocomletetextview中删除焦点时,它将正确的文本设置为autocompletetext字段。另一个问题是,当我在autocompletetextview的自定义适配器中填充列表时,我想获取autocompletetext字段中所选文本的id,我正在关联此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。我不需要我需要markaz id,我用markazList传递。 请帮助我,任何帮助都应该非常感激。
答案 0 :(得分:0)
试试这个。
autoComMarker.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
Markaz items= adapter.getItem(position);
//Now you can get items of Markaz from items.get...
}
});
答案 1 :(得分:0)
您必须获取position
中所选项目的listview
。在autoComMarker.setOnItemClickListener()
:
Markaz items= adapter.getItem(position);