You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AutoCompleteTextView选择下拉项时,如何获取对应ContactId(code)?

嘿,我看你现在的问题核心是——你的AutoCustomAdapter只存了name字符串,没法和对应的code(也就是ContactId)关联起来对吧?别担心,咱们只需要调整一下适配器的数据源类型,让它携带完整的Model信息,就能轻松解决这个问题。

具体解决方案

1. 确认你的Model类完整性

先确保你的Model类(比如命名为ContactModel)包含name和code字段,以及对应的getter方法(方便后续获取数据):

public class ContactModel {
    private String name;
    private String code; // 对应你的ContactId

    public ContactModel(String name, String code) {
        this.name = name;
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public String getCode() {
        return code;
    }
}

2. 修改自定义适配器,适配Model类

把原来的ArrayAdapter<String>改成ArrayAdapter<ContactModel>,让每个列表项同时保存name和code的关联信息:

private class AutoCustomAdapter extends ArrayAdapter<ContactModel> {
    private ArrayList<ContactModel> suggestions;

    public AutoCustomAdapter() {
        super(context, android.R.layout.simple_dropdown_item_1line);
        suggestions = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return suggestions.size();
    }

    @Override
    public ContactModel getItem(int position) {
        return suggestions.get(position);
    }

    // 重写getView,让下拉列表显示name字段
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView textView = view.findViewById(android.R.id.text1);
        textView.setText(getItem(position).getName());
        return view;
    }

    // 补充过滤逻辑(如果你的适配器需要根据输入筛选数据)
    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                ArrayList<ContactModel> filteredList = new ArrayList<>();
                // 假设你有一个全局存储所有API返回数据的列表allContacts
                if (constraint == null || constraint.length() == 0) {
                    filteredList.addAll(allContacts);
                } else {
                    String filterPattern = constraint.toString().toLowerCase().trim();
                    for (ContactModel item : allContacts) {
                        if (item.getName().toLowerCase().contains(filterPattern)) {
                            filteredList.add(item);
                        }
                    }
                }
                results.values = filteredList;
                results.count = filteredList.size();
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                suggestions.clear();
                suggestions.addAll((ArrayList<ContactModel>) results.values);
                notifyDataSetChanged();
            }
        };
    }
}

3. 设置点击监听器,获取选中的code

在onCreate方法中,给AutoCompleteTextView添加点击事件监听器,选中时直接获取完整的Model对象并提取code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    AutoCompleteTextView edtName = findViewById(R.id.edtName);
    AutoCustomAdapter adapter = new AutoCustomAdapter();
    edtName.setAdapter(adapter);

    // 监听下拉列表选中事件
    edtName.setOnItemClickListener((parent, view, position, id) -> {
        // 获取选中的ContactModel对象
        ContactModel selectedContact = (ContactModel) parent.getItemAtPosition(position);
        // 提取对应的code(ContactId)
        String contactId = selectedContact.getCode();
        // 这里可以根据需求使用contactId做后续操作
        Toast.makeText(context, "选中的ContactId:" + contactId, Toast.LENGTH_SHORT).show();
    });
}

关键说明

  • 原来的适配器只存储String类型的name,丢失了与code的关联关系,换成Model类后,每一项都能完整携带两个字段的信息
  • 重写getView方法是为了让下拉列表显示用户可见的name,而内部实际存储的是完整的Model对象
  • 过滤逻辑需要同步调整为操作Model列表,确保筛选后的项依然保留code信息

内容的提问来源于stack exchange,提问作者Arnold Brown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:37:18