how to implement search in custom listview in android?

后端 未结 2 424
悲哀的现实
悲哀的现实 2021-01-07 01:37

I have an edittext and a listview in my application my listview show contact list. I want listview filter with edittext. I searched a lot on google and found some examles bu

相关标签:
2条回答
  • 2021-01-07 01:45

    Android has an built in search bar feature .

    • You can use this search bar to get input from the user .Can refer to this link link2
    • Create an async task which will query your database/data to fetch matching results and populate the listview again

    Step 1 Add the following in your manifest :

    <application
                android:allowBackup="true"
                android:icon="@drawable/dolphin1"
                android:label="@string/app_name"
                android:theme="@style/AppTheme" >
                <activity
                    android:name="com.RareMediaCompany.MuditaBulkScanner.DocketListActivity"
                    android:label="@string/app_name"
                    android:launchMode="singleTop"
                    android:screenOrientation="portrait" >
                    <intent-filter>
                        <action android:name="android.intent.action.SEARCH" />
                    </intent-filter>
                    <intent-filter>
                        <action android:name="android.intent.action.VIEW" />
                    </intent-filter>
    
                    <meta-data
                        android:name="android.app.searchable"
                        android:resource="@xml/searchable" />
                </activity>
    
                <meta-data
                    android:name="android.app.default_searchable"
                    android:value="com.RareMediaCompany.MuditaBulkScanner.DocketListActivity" />
            </application>
    

    Step2

    onSearchRequested(); will call your search bar .You can add this in on click of your search button.

    Step3 Simply call this function in your activity's on create :

    private void handleIntent(Intent intent) {
    
             if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
                myList.clear();
    
                String query = intent.getStringExtra(SearchManager.QUERY);
    
                new getQueryResultAndPopulateListTask(this, query).execute();
            }
    
        }
    
    0 讨论(0)
  • 2021-01-07 01:46

    This one worked for me:

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
               int textlength = cs.length();
               ArrayList<ContactStock> tempArrayList = new ArrayList<ContactStock>();
               for(ContactStock c: arraylist){
                  if (textlength <= c.getName().length()) {
                     if (c.getName().toLowerCase().contains(cs.toString().toLowerCase())) {
                        tempArrayList.add(c);
                     }
                  }
               }
               mAdapter = new ContactListAdapter(activity, tempArrayList);
               lv.setAdapter(mAdapter);
         }
    
    0 讨论(0)
提交回复
热议问题