如何搜索与Android设备上编辑文本列表视图

如何搜索与Android设备上编辑文本列表视图

本文介绍了如何搜索与Android设备上编辑文本列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过写在的EditText 的东西搜索的ListView 。就像如果我写的第一个单词苏所以在的ListView内容与前两个词作为苏应该在上面。

这是我的code:

 西尔斯=(AutoCompleteTextView)findViewById(R.id.Search);
    Sear.setThreshold(1);    Sear.addTextChangedListener(新TextWatcher(){        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
            的System.out.println(文本[+ S +]);            。sdAdapter.getFilter()过滤(s.toString());
        }        @覆盖        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                INT后){        }        @覆盖
        公共无效afterTextChanged(编辑S){
        }
    });

现在,当我写我的的EditText 的ListView 自败的东西。
我有我的的ListView 5列。我想搜索特定人的姓名和搜索的ListView 使用该名称。

这是适配器:

  sdAdapter =新StationDetails_Adapter(AllStations.this,R.layout.listview_row2,Stationlist);


解决方案

的ListView搜索可与当地的SQLite数据库完成。我想你shuld对SQLite数据库知识,然后按照步骤如下:

1)设置的ListView 适配器和存储在SQLite数据库的所有数据

2)写在DB年底像一个简单的查询: SELECT * FROM YOUR_TABLE_NAME,其中列名LIKE'%+ your_search_keyword +%

3)当你在执行这个查询 onTextChanged()方法。

4)使用光标获得的SQLite数据库对搜索关键字的所有数据和设置的ListView 适配器

5)如果光标不返回任何数据意味着你对搜索关键字的任何记录。

其他方式:其他比的SQLite数据库可以设置适配器和过滤器像一个特定的搜索关键字:

例如:

 字符串产品[] = {戴尔Inspiron,HTC One X的,HTC野火S,HTC Sense的,HTC感觉XE
                                    iPhone 4S的,三星Galaxy Note 800
                                    三星Galaxy S3,MacBook Air的,Mac Mini的,MacBook Pro的};            LV =(ListView控件)findViewById(R.id.list_view);
            inputSearch =(EditText上)findViewById(R.id.inputSearch);            //添加项目列表视图
            适配器=新ArrayAdapter<串GT;(这一点,R.layout.list_item,R.id.product_name,产品);
            lv.setAdapter(适配器);inputSearch.addTextChangedListener(新TextWatcher(){    @覆盖
    公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,INT ARG3){
        //当用户更改文本
        。MainActivity.this.adapter.getFilter()过滤器(CS);
    }    @覆盖
    公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
            INT ARG3){
        // TODO自动生成方法存根    }    @覆盖
    公共无效afterTextChanged(编辑为arg0){
        // TODO自动生成方法存根
    }
});

上面的例子是从

I want to search ListView by writing something inside the EditText. like if I write first to words "Su" so the content in ListView with first two words as "Su" should come on top.

This is my code:

Sear = (AutoCompleteTextView) findViewById(R.id.Search);
    Sear.setThreshold(1);

    Sear.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            System.out.println("Text ["+s+"]");

            sdAdapter.getFilter().filter(s.toString());
        }

        @Override

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

Right now when I write something in my EditText the ListView dissapears.I am having 5 columns in my ListView. i want to search the name of that specific person and search the ListView with that name.

This is the Adapter:

sdAdapter = new StationDetails_Adapter(AllStations.this, R.layout.listview_row2 , Stationlist);
解决方案

Searching in a ListView can be done with local SQLite DB. I suppose you shuld have knowledge about SQLite Database then follow the steps like:

1) Set ListView adapter and store all data in SQLite Database

2) write a simple query on DB end like: select * from YOUR_TABLE_NAME where column_name like '%+your_search_keyword+%'

3)execute this query while you are in onTextChanged() method.

4) use cursor to get all data against search keyword from SQLite DB and set the ListView adapter

5) If cursor do not return any data means you have no record against search keyword.

Other Way: Other than SQLite DB you can set adapter and filter on a specific search keyword like:

For Example:

    String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                                    "iPhone 4S", "Samsung Galaxy Note 800",
                                    "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

            lv = (ListView) findViewById(R.id.list_view);
            inputSearch = (EditText) findViewById(R.id.inputSearch);

            // Adding items to listview
            adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
            lv.setAdapter(adapter);

inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.this.adapter.getFilter().filter(cs);
    }

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

    }

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

The above example is referred from Android Hive Tutorial

这篇关于如何搜索与Android设备上编辑文本列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:37