我正在创建一个具有autocompletextview
的应用,其中用户将看到类似组合框的类型的建议。
我所取得的成就:
我需要完成的事情:
现在我遇到了点击项目的当前信息(其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;
}