सबसे पहले, आपको एक XML लेआउट बनाने की आवश्यकता है जिसमें एक EditText और एक ListView दोनों हैं।
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Pretty hint text, and maxLines -->
<EditText android:id="@+building_list/search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
यह सब कुछ ठीक से बाहर कर देगा, ListView के ऊपर एक अच्छा EditText के साथ। इसके बाद, सामान्य रूप से एक ListActivity बनाएं, लेकिन विधि setContentView()
में एक कॉल जोड़ें onCreate()
ताकि हम अपने हाल ही में घोषित लेआउट का उपयोग करें। याद रखें कि हमने ListView
विशेष रूप से ID'ed किया है android:id="@android:id/list"
। यह ListActivity
जानने की अनुमति देता है कि ListView
हम अपने घोषित लेआउट में किसका उपयोग करना चाहते हैं।
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filterable_listview);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getStringArrayList());
}
ऐप को चलाना अब अपने पिछले को दिखाना चाहिए ListView
, ऊपर एक अच्छे बॉक्स के साथ। उस बॉक्स को कुछ करने के लिए, हमें उससे इनपुट लेना होगा, और उस इनपुट को सूची को फ़िल्टर करना होगा। जबकि बहुत से लोगों ने मैन्युअल रूप से ऐसा करने की कोशिश की है, ज्यादातर ListView
Adapter
कक्षाएं एक Filter
ऑब्जेक्ट के साथ आती हैं जिसका उपयोग स्वचालित रूप से फ़िल्टर करने के लिए किया जा सकता है। हमें बस इनपुट को अंदर से पाइप करना EditText
होगा Filter
। पता चला कि यह बहुत आसान है। एक त्वरित परीक्षण चलाने के लिए, इस लाइन को अपने onCreate()
कॉल में जोड़ें
adapter.getFilter().filter(s);
ध्यान दें कि ListAdapter
इस काम को करने के लिए आपको अपने एक वैरिएबल को सहेजना होगा - मैंने अपने ArrayAdapter<String>
पहले से 'एडेप्टर' नामक वैरिएबल में सेव कर लिया है ।
अगला कदम इनपुट से प्राप्त करना है EditText
। यह वास्तव में एक सा लगता है। आप OnKeyListener()
अपने को जोड़ सकते हैं EditText
। हालाँकि, यह श्रोता केवल कुछ प्रमुख घटनाओं को प्राप्त करता है । उदाहरण के लिए, यदि कोई उपयोगकर्ता 'wyw' में प्रवेश करता है, तो भविष्य कहनेवाला पाठ 'आंख' की सिफारिश करेगा। जब तक उपयोगकर्ता या तो 'wyw' या 'eye' चुनता है, तब तक आपको OnKeyListener
एक महत्वपूर्ण ईवेंट प्राप्त नहीं होगा। कुछ इस समाधान को पसंद कर सकते हैं, लेकिन मैंने इसे निराशाजनक पाया। मैं हर महत्वपूर्ण घटना चाहता था, इसलिए मेरे पास फ़िल्टरिंग या फ़िल्टरिंग नहीं करने का विकल्प था। समाधान एक है TextWatcher
। बस हर बार टेक्स्ट में बदलाव TextWatcher
करने और जोड़ने के EditText
लिए ListAdapter
Filter
एक फ़िल्टर अनुरोध करें और पास करें । दूर करने के लिए याद रखें TextWatcher
में OnDestroy()
! यहाँ अंतिम समाधान है:
private EditText filterText = null;
ArrayAdapter<String> adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filterable_listview);
filterText = (EditText) findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getStringArrayList());
}
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
filterText.removeTextChangedListener(filterTextWatcher);
}