implement the search function for listview in fragments

advertisements

I'm setting some values taken from a database i created into a listview. The listview is populated correctly.Now i want to implement a search function to my listview. How can i do this? I've already created search bar layout.I've referred to many tutorials but could not get it done.

My class

public class StockWatchView extends Fragment {

    private ListView mListView;
    private LazyListViewAdapter mAdapter;
    private EditText search;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.stock_watchlist_view, container, false);

        mListView = (ListView) view.findViewById(R.id.listview);
        search = (EditText) view.findViewById(R.id.search);

        Toast.makeText(StockWatchView.this.getActivity(), "Do Background Service", Toast.LENGTH_LONG).show();

        DBHandler dbHandler = new DBHandler(StockWatchView.this.getActivity(), null, null, 0);

        ArrayList<BaseElement> items = dbHandler.getAllPric();

        if (items.size() != 0) {
            mAdapter = new LazyListViewAdapter(items, StockWatchView.this.getActivity(), ListElement.PRICE_LIST.getPosition());

            mListView.setAdapter(mAdapter);

            Toast.makeText(this.getActivity(), "Toast " + dbHandler.getLastUpdateTime(), Toast.LENGTH_LONG).show();

        } else {

            //new BackgroundService().execute();

        }

        search.addTextChangedListener(new TextWatcher() {

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

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {

            }
            // TODO Auto-generated method stub

        });
       return view;
    }
}


You will have to declare a text watcher like::

                    TextWatcher mSearchTw = new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        //put length after which this would be called
                        if (editable.length() > 2) {
                        //here you search in db
                        // or the stuff you want to do in view
                        }
                    }
                }
`               editText.addTextChangedListener(mSearchTw);