• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

自定义ArrayAdapter AutoCompleteTextView不触发

android-arrayadapter 来源:rstambach 8次浏览

自定义ArrayAdapter AutoCompleteTextView不触发 我想与一个ArrayList的对象使用自定义列表适配器使用AutoCompleteTextView,我无法获得自动完成列表显示。这里是我的代码: activity_main.xml中自定义ArrayAdapter AutoCompleteTextView不触发

<?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;} 
    } 

有一个在代码中没有错误,但它并不显示自动完成选择,当我进入了customerACTV字母。 CustomerListAdapter构造函数正在被调用,并且Customer对象的ArrayList正在通过,但CustomerListAdapter的getView没有被调用。

感谢


===========解决方案如下:

对于汽车的建议工作,在自动完成的TextView,我们必须为忽略getFilter(),在这里我们设置过滤功能,默认的适配器只能过滤字符串,并且您正在使用一个对象Customer

所以,你需要做你以下几点:

@NonNull 
@Override 
public Filter getFilter() { 

    return new Filter() { 
     final Object object = new Object(); 

     @Override 
     public String convertResultToString(Object resultValue) { 
     return resultValue.toString(); 

     } 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 




      FilterResults filterResults = new FilterResults(); 
      List<Customer> customers = new ArrayList<>(); 

      if (constraint != null) { 

       for (Customer customer : customerList) { 
        if (department instanceof SearchableStrings) { 
         if (customer.getname().toLowerCase().contains(constraint.toString().toLowerCase())) { 
          customers.add(customer); 

         } 

        } 
        if (customers.size() > 1) { 
         filterResults.values = customers; 
         filterResults.count =  customers.size(); 
        } else { 
         filterResults.values = null; 
         filterResults.count = 0; 


       } 



      } 
      //notifyDataSetChanged(); 
      return filterResults; 
     } 

     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 

      // mObjects.clear(); 
      if (results != null && results.count > 0) { 
       clear(); 
       // avoids unchecked cast warning when using customerList.addAll((ArrayList<Customer>) results.values); 
       for (Object object : (List<?>) results.values) { 
        add(object); 


       } 
       notifyDataSetChanged(); 
      } 
     } 


    }; 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)