无法获取AutoCompleteTextView自定义适配器的onItemClick上的值

时间:2018-02-19 12:15:09

标签: android android-arrayadapter autocompletetextview

我正在创建一个具有autocompletextview的应用,其中用户将看到类似组合框的类型的建议。

  

我需要完成的任务。

     
      
  • 为AutoCompleteTextView创建自定义适配器。 (完成)
  •   
  • 当我输入文字时,会显示建议列表。 (完成)
  •   
  • 当我点击该建议时,我可以将其当前名称与其中列出的其他信息相同。
  •   

现在我遇到了点击项目的当前信息(其ID,姓名,电话号码,电子邮件)的问题。当我点击一个项目时,会显示第一个项目的当前数据。

这是我的吸气者和二传手:

public class Contacts {

    private String id;
    private String name;
    private String email;
    private String number;

    public Contacts() {
    }

    public Contacts(String id, String name, String email, String number) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.number = number;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}

这是我的带有过滤器的自定义适配器:

public class AutoCompleteTextViewAdapter extends ArrayAdapter<Contacts> {

        private List<Contacts> contacts = new ArrayList<Contacts>();
        private List<Contacts> filteredContacts = new ArrayList<Contacts>();

        public AutoCompleteTextViewAdapter(Context context, List<Contacts> contacts) {

            super(context, 0, contacts);
            this.contacts = contacts;

        }

        public int getCount() {
            return filteredContacts.size();
        }

        @NonNull
        @Override
        public Filter getFilter() {
            return new ContactsFilter(this, contacts);
        }



        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            Contacts contacts = filteredContacts.get(position);
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.inflater_search_contacts_spinner, parent, false);
            TextView nameTV = convertView.findViewById(R.id.phoneNameTV);
            TextView numberTV = convertView.findViewById(R.id.phoneNumberTV);
            nameTV.setText(contacts.getName());
            numberTV.setText(contacts.getNumber());

            return convertView;

        }

        private class ContactsFilter extends Filter {

            AutoCompleteTextViewAdapter adapter;
            List<Contacts> originalList;
            List<Contacts> filteredList;

            public ContactsFilter(AutoCompleteTextViewAdapter adapter, List<Contacts> originalList) {

                super();
                this.adapter = adapter;
                this.originalList = originalList;
                this.filteredList = new ArrayList<>();

            }

            protected FilterResults performFiltering(CharSequence constraint) {

                filteredList.clear();
                final FilterResults results = new FilterResults();
                if (constraint == null || constraint.length() == 0) {
                    filteredList.addAll(originalList);
                } else {
                    final String filterPattern = constraint.toString().toLowerCase().trim();
                    for (final Contacts contacts : originalList) {

                        if(contacts.getName().toLowerCase().contains(filterPattern)) {
                            filteredList.add(contacts);
                        }

                    }
                }

                results.values = filteredList;
                results.count = filteredList.size();
                return results;

            }

            protected void publishResults(CharSequence constraint,FilterResults results) {

                adapter.filteredContacts.clear();
                adapter.filteredContacts.addAll((List) results.values);
                adapter.notifyDataSetChanged();

            }

        }
    }

这是我的oncreate方法中的代码:

searchContactsET = findViewById(R.id.searchContactsET); //autocompletextview
searchContactsET.setThreshold(1);
searchContactsET.setAdapter(new AutoCompleteTextViewAdapter(getApplicationContext(), getAllContacts()));
searchContactsET.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Contacts contacts = (Contacts) adapterView.getItemAtPosition(i);
        searchContactsET.setText(contacts.getName());
        Log.i("CONTACTS: ", contacts.getName());
    }
});

我的oncreate方法在同一个类下的列表。

public List<Contacts> getAllContacts() {
    List<Contacts> contacts = new ArrayList<Contacts>();
    contacts.add(new Contacts("PO90", "ERIKA", "erikasong1925@gmail.com", "09324612629"));
    contacts.add(new Contacts("PO95", "JULIEZETTE", "juliesong1925@gmail.com", "09284612629"));
    return contacts;
}

1 个答案:

答案 0 :(得分:0)

可能的替代方法是将ArrayList存储在Activity中。 从项目选择位置获取Arraylist成员。

您可以在活动片段中声明列出联系人。 使用下面的

修改getContact方法
public List<Contacts> getAllContacts() {
    contacts = new ArrayList<Contacts>();
    contacts.add(new Contacts("PO90", "ERIKA", "erikasong1925@gmail.com", "09324612629"));
    contacts.add(new Contacts("PO95", "JULIEZETTE", "juliesong1925@gmail.com", "09284612629"));
    return contacts;
}

使用以下代码修改项目选择的方法

searchContactsET = findViewById(R.id.searchContactsET); //autocompletextview
searchContactsET.setThreshold(1);
searchContactsET.setAdapter(new AutoCompleteTextViewAdapter(getApplicationContext(), getAllContacts()));
searchContactsET.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Contacts contact = contacts.get(i);
        searchContactsET.setText(contacts.getName());
        Log.i("CONTACTS: ", contacts.getName());
    }
});

我认为这会解决您的问题。