3

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 but none worked for me here's my code
my custom adapter

public class ContactListAdapter extends ArrayAdapter {

    private final Activity activity;
    private final List<ContactStock> stocks;
    private ArrayList<ContactStock> arraylist;

    public ContactListAdapter(Activity activity, List<ContactStock> objects) {
        super(activity, R.layout.listview_detail, objects);
        this.activity = activity;
        this.stocks = objects;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View rowView = convertView;
        ContactStockView sv = null;
        if (rowView == null) {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(
                    R.layout.listview_detail, null);

            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            sv = new ContactStockView();
            sv.name = (TextView) rowView.findViewById(R.id.textView1);
            sv.number = (TextView) rowView.findViewById(R.id.textView2);

            // Cache the view objects in the tag,
            // so they can be re-accessed later
            rowView.setTag(sv);
        } else {
            sv = (ContactStockView) rowView.getTag();
        }
        // Transfer the stock data from the data object
        // to the view objects
        ContactStock currentStock = (ContactStock) stocks.get(position);
        sv.name.setText(currentStock.getName());
        sv.number.setText(currentStock.getNumber());

        // TODO Auto-generated method stub
        return rowView;
    }

    protected static class ContactStockView {
        protected TextView name;
        protected TextView number;
    }

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        stocks.clear();
        if (charText.length() == 0) {
            stocks.addAll(arraylist);
        } else {
            for (ContactStock cs : arraylist) {
                if (cs.getName().contains(charText)) {
                    stocks.add(cs);
                }
            }
        }
        notifyDataSetChanged();
    }
}

Main class edittext code is

edittext = (EditText)findViewById(R.id.editText1);
        edittext.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                //MainActivity.this.adapt.getFilter().filter(s);
                 String searchString=edittext.getText().toString();
                 adapt.filter(searchString);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

without custom adapter it is working with getfilter(). But I don't know how to filter with custom adapter. any help will be appriciated. thanks in advance

soyeb84
  • 72
  • 1
  • 9
  • possible duplicate of [Search functionality in Android Custom ListView](http://stackoverflow.com/questions/18460139/search-functionality-in-android-custom-listview) – 2Dee Feb 17 '14 at 11:20
  • I got solution check bellow link http://stackoverflow.com/questions/21475725/android-filter-listview-custom-adapter/23671338#23671338 – nirav kalola May 15 '14 at 07:07

2 Answers2

34

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);
     }
Ali
  • 9,800
  • 19
  • 72
  • 152
  • Ali your answer work but after some edit. anyhow i appreciate your effort +1 for you –  Feb 17 '14 at 11:50
  • Simpler than using Filter approach. Works very well for small data sets. Thanks! – AlexVPerl Aug 10 '14 at 21:11
  • Instead of creating new ArrayList Each time when onTextChanged() is called, should call tempArrayList.clear(); and initialize it only once at onCreate(). Output is same though it saves memory. – Jayant Arora Mar 18 '15 at 14:45
1

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();
        }

    }
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66