2016-01-28 6 views
5

मैं AutoCompleteTextView में डिफ़ॉल्ट छानने परिवर्तन करना चाहते हैं। डिफॉल्ट फ़िल्टरिंग उन सभी तारों को पाती है जो दिए गए टोकन के साथ शुरू होती हैं। मेरी प्रोजेक्ट की आवश्यकता है कि फ़िल्टरिंग को उन सभी तारों को ढूंढना चाहिए जिनमें दिए गए टोकन हैं।AutoCompleteTextView बदलें फिल्टर?

क्या यह संभव है?

+0

Pls ने मेरा जवाब http://stackoverflow.com/questions/32926034/autocompletetextview-not-completing-words-inside-parentheses/32928446#32928446 – BNK

+0

इस समाधान को भी देखें: http: // stackoverflow। com/एक/37298258/1808829 –

उत्तर

13

मुझे इसके लिए एक समाधान मिला, धन्यवाद Google और दो दिनों के लिए खोज। जैसा कि @ torque203 ने सुझाव दिया है, मैंने अपना कस्टम एडाप्टर लागू किया है। सबसे पहले अनुकूलक में कस्टम आइटम के लिए एक नया एक्सएमएल फ़ाइल को परिभाषित:

autocomplete_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp" 
     android:paddingTop="16dp" 
     android:paddingBottom="16dp" 
     android:id="@+id/lbl_name" /> 
</RelativeLayout> 

अपने नाम के लिए नया वर्ग बनाएँ:

नाम

public class Names { 
    public String name; 
} 

NamesAdapter

public class NamesAdapter extends ArrayAdapter<Names> { 

    Context context; 
    int resource, textViewResourceId; 
    List<Names> items, tempItems, suggestions; 

    public NamesAdapter(Context context, int resource, int textViewResourceId, List<Names> items) { 
     super(context, resource, textViewResourceId, items); 
     this.context = context; 
     this.resource = resource; 
     this.textViewResourceId = textViewResourceId; 
     this.items = items; 
     tempItems = new ArrayList<Names>(items); // this makes the difference. 
     suggestions = new ArrayList<Names>(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = inflater.inflate(R.layout.autocomplete_item, parent, false); 
     } 
     Names names = items.get(position); 
     if (names != null) { 
      TextView lblName = (TextView) view.findViewById(R.id.lbl_name); 
      if (lblName != null) 
       lblName.setText(names.name); 
     } 
     return view; 
    } 

    @Override 
    public Filter getFilter() { 
     return nameFilter; 
    } 

    /** 
    * Custom Filter implementation for custom suggestions we provide. 
    */ 
    Filter nameFilter = new Filter() { 
     @Override 
     public CharSequence convertResultToString(Object resultValue) { 
      String str = ((Names) resultValue).name; 
      return str; 
     } 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      if (constraint != null) { 
       suggestions.clear(); 
       for (Names names : tempItems) { 
        if (names.name.toLowerCase().contains(constraint.toString().toLowerCase())) { 
         suggestions.add(names); 
        } 
       } 
       FilterResults filterResults = new FilterResults(); 
       filterResults.values = suggestions; 
       filterResults.count = suggestions.size(); 
       return filterResults; 
      } else { 
       return new FilterResults(); 
      } 
     } 

     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      List<Names> filterList = (ArrayList<Names>) results.values; 
      if (results != null && results.count > 0) { 
       clear(); 
       for (Names names : filterList) { 
        add(names); 
        notifyDataSetChanged(); 
       } 
      } 
     } 
    }; 
} 

SearchActivity (या अपने मुख्य गतिविधि)

.... 
    List<Names> namesList = //your names list; 
    NamesAdapter namesAdapter = new NamesAdapter(
        SearchActivity.this, 
        R.layout.activity_search, 
        R.id.lbl_name, 
        namesList 
      ); 
      //set adapter into listStudent 
      autoCompleteTextView.setAdapter(namesAdapter); 
      autoCompleteTextView.showDropDown(); 
... 
+1

यह बहुत आशाजनक दिखता है। –

+0

'' '' '' publishResults''' में '' 'clear''' विधि के लिए UnsupportedOperationException'''। – JCarlos

+0

@JCarlos https: // stackoverflow।कॉम/ए/18685928/5222156 – madim

0

हां, यह संभव है।

सबसे पहले जिस तरह से:

आप जो ListAdapter और Filterable लागू करता है एक कस्टम एडाप्टर बनाना चाहिए।

Filter आपके "इसमें" फ़िल्टर तर्क को लागू करने से अधिक है।

और आप इस एडाप्टर को एडाप्टर के रूप में अपने AutoCompleteTextView पर सेट करते हैं।

दूसरा तरीका:

आप पहले से ही ArrayAdapter उपयोग कर रहे हैं। आप इसे getFilter() विधि को ओवरराइड कर सकते हैं।

0

बस Caffe लैटे का अच्छा जवाब पर विस्तार करने के लिए:

1) autoCompleteTextView.showDropDown(); जरूरत नहीं है।

//retrieve the input in the autoCompleteTextView 
     autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      //parent The AdapterView where the click happened. 
      //view The view within the AdapterView that was clicked (this will be a view provided by the adapter) 
      //position The position of the view in the adapter 
      //id The row id of the item that was clicked. 
      public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) { 
       String selection =parent.getItemAtPosition(position).toString(); 
       Toast.makeText(parent.getContext(),"" + selection,Toast.LENGTH_SHORT).show(); 
      } 
     }); 

वस्तु माता पिता से लिया गया एक toString() पद्धति लागू होना चाहिए:

2) इनपुट वस्तु उपयोग कर सकते हैं प्राप्त करने के लिए।

संबंधित मुद्दे