Custom ArrayAdapter AutoCompleteTextView not triggering

时间:2017-12-18 05:03:49

标签: android-arrayadapter autocompletetextview

Custom ArrayAdapter AutoCompleteTextView not triggering I’m trying to use a AutoCompleteTextView with a ArrayList of objects using a custom list adapter and I cannot get the auto complete list to display. Here is my code: activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="rick.customarrayadpter.MainActivity">

        <AutoCompleteTextView
            android:id="@+id/customerACTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="search" />
    </android.support.constraint.ConstraintLayout>

MainActivity.java
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.AutoCompleteTextView;

    import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity {
        private ArrayList<Customer> customerList;

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

            customerList = new ArrayList<Customer>();
            customerList.add(new Customer(111, "Pete"));
            customerList.add(new Customer(222, "Bob"));
            customerList.add(new Customer(333, "Paul"));
            customerList.add(new Customer(444, "Tom"));
            customerList.add(new Customer(555, "Jane"));
            customerList.add(new Customer(666, "Susan"));

            CustomerListAdapter customerListAdapter = new CustomerListAdapter(this, R.layout.support_simple_spinner_dropdown_item, R.id.customerACTV, customerList);
            AutoCompleteTextView customerACTV = (AutoCompleteTextView)findViewById(R.id.customerACTV);
            customerACTV.setThreshold(1);
            customerACTV.setAdapter(customerListAdapter);

        }

    }

CustomerListAdapter.java
    public class CustomerListAdapter extends ArrayAdapter<Customer> {

            ArrayList<Customer> customerList = new ArrayList<>();

            public CustomerListAdapter(Context context, int resource,int textViewResourceId,  ArrayList<Customer> objects) {
                super(context, resource, textViewResourceId, objects);
                customerList = objects;
            }

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

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

                View v = convertView;
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.list_items, null);
                AutoCompleteTextView textView = (AutoCompleteTextView) v.findViewById(R.id.customerACTV);
                textView.setText(customerList.get(position).getName());
                return v;

            }
        }

list_items.xml
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"

            android:text="Demo"
            android:textColor="#000" />


    </android.support.constraint.ConstraintLayout>

    Customer.java
    public class Customer {
        String name;
        int id;
        public Customer(int ID, String Name){
            this.id = ID;
            this.name = Name;
        }

        public String getName(){return this.name;}
        public int getId(){return this.id;}
    }

There is no errors in the code but it does not show the auto complete selection when I enter letters in the customerACTV. The CustomerListAdapter constructor is getting called and the ArrayList of the Customer objects is getting passed but the getView of the CustomerListAdapter is not getting called.

Thanks

3 个答案:

答案 0 :(得分:1)

要使自动建议正常工作,在自动填充文本视图中,我们必须覆盖getFilter(),我们设置过滤器功能,默认适配器只能过滤字符串,并且您正在使用对象{{1} }

所以你需要在你身上做到以下几点:

Customer

答案 1 :(得分:0)

public class CustomerListAdapter extends ArrayAdapter<Customer> {

ArrayList<Customer> customerList, suggestionList;


public CustomerListAdapter(Context context, int resource, int textViewResourceId, ArrayList<Customer> customerList) {
    super(context, resource, textViewResourceId, customerList);
    this.customerList = new ArrayList<>();
    this.customerList = customerList;

}

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

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

    View view = convertView;
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list_items, parent, false);
    }

    Customer customer = customerList.get(position);
    if(customer != null){
        TextView textView = view.findViewById(R.id.textView);
        if(textView != null)
            textView.setText(customer.getName());
    }
    return view;
}



@NonNull
@Override
public Filter getFilter() {
    return new Filter() {
        final Object object = new Object();

        @Override
        public String convertResultToString(Object resultValue) {
            Customer customer = (Customer) resultValue;
            return customer.getName();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {


            FilterResults filterResults = new FilterResults();
            suggestionList = new ArrayList<>();
            if (constraint != null) {
                for (Customer customer : customerList) {

                    if (customer.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                        suggestionList.add(customer);
                    }
                    if (customerList.size() > 1) {
                        filterResults.values = suggestionList;
                        filterResults.count = suggestionList.size();
                    } else {
                        filterResults.values = null;
                        filterResults.count = 0;
                    }
                }
            }
            return filterResults;
        }

        @Override
        protected void publishResults (CharSequence constraint, FilterResults results){
            ArrayList<Customer> filterList = (ArrayList<Customer>)results.values;

            if (results != null && results.count > 0) {
                clear();
                for (Customer customer : filterList) {
                    add(customer);
                    notifyDataSetChanged();
                }
                notifyDataSetChanged();
            }
        }
    };
}

}

答案 2 :(得分:0)

公共类CustomerListAdapter扩展了ArrayAdapter {

private ArrayList<Customer> customerList;
private ArrayList<Customer> filteredList;

public CustomerListAdapter(Context context, int resource, int textViewResourceId, ArrayList<Customer> customerList) {
    super(context, resource, textViewResourceId, customerList);
    this.customerList = new ArrayList<Customer>(customerList);
    this.filteredList = customerList;
}

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

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

    View view = convertView;
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list_items, parent, false);
    }

    Customer customer = filteredList.get(position);
    if(customer != null){
        TextView textView = view.findViewById(R.id.textView);
        if(textView != null)
            textView.setText(customer.getName());
    }
    return view;
}

@NonNull
@Override
public Filter getFilter() {
    return new Filter() {
        final Object object = new Object();

        @Override
        public String convertResultToString(Object resultValue) {
            Customer customer = (Customer) resultValue;
            return customer.getName();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            ArrayList<Customer> filters = new ArrayList<Customer>();
            if (constraint != null && constraint.length()>0) {
                for (Customer customer : customerList) {

                    if (customer.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                        filters.add(customer);
                    }
                 }
                filterResults.values = filters;
                filterResults.count = filters.size();
                filteredList = filters;
            }
            return filterResults;
        }

        @Override
        protected void publishResults (CharSequence constraint, FilterResults results){
            ArrayList<Customer> filterList = (ArrayList<Customer>)results.values;

            if (results != null && results.count > 0) {
                clear();
                for (Customer customer : filterList) {
                    add(customer);
                    notifyDataSetChanged();
                }
                notifyDataSetChanged();
            }
        }
    };
}

}