在片段中实现listview的搜索功能

时间:2015-06-18 04:56:12

标签: android search android-fragments android-listview

我将从创建的数据库中获取的值设置为列表视图。列表视图已正确填充。现在我想在listview中实现搜索功能。我怎样才能做到这一点?我已经创建了搜索栏布局。我已经参考了许多教程,但无法完成。

我的班级

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

2 个答案:

答案 0 :(得分:0)

您必须声明一个文本观察器,如::

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

答案 1 :(得分:0)

items ArrayList公之于众。

然后通过将可编辑文本作为参数传递,通过查询数据库,就像在OnTextChanged()方法中ArrayList<BaseElement>一样,获取所需数据(搜索结果类型为dbHandler.getAllPric();)。

在查询中,您将使用“like”短语来获取适当的数据。

例如,

items = dbHandler.getSearchResult(editableText);//editableText as String

执行adapter.notifyDataSetChanged();

之后

希望这种方法对你有所帮助。