如何从一个ListView使用textwatcher搜索数据? [英] How to search data from a listview using textwatcher?

查看:105
本文介绍了如何从一个ListView使用textwatcher搜索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了该数据来自一个字符串数组存储在 string.xml 一个列表视图。

我有一个EditText上,我想从我的列表视图中搜索特定的数据。我的列表视图数据中包含这样的011新德里德里......我该怎么做?

下面是我的code ..

 适配器=新的ArrayAdapter<字符串>(这一点,
            android.R.layout.simple_list_item_1,ArrayList的);

资源RES = getResources();
的String [] A = res.getStringArray(R.array.states);
arrayList.add(+α);
adapter.notifyDataSetChanged();

editText.addTextChangedListener(新TextWatcher(){
    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
                              诠释计数){
        // TODO自动生成方法存根
    }

    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                                  之后INT){
        // TODO自动生成方法存根
    }

    公共无效afterTextChanged(编辑S){
        // TODO自动生成方法存根
    }
});
 

解决方案

我希望下面的code为你工作。

 公共类MainActivity延伸活动{
    私人的ListView LV;
    私人的EditText等;
    字符串listview_array [] = {123,二,戴尔Inspiron,HTC One X的,HTC野火S,的HTC Sense,1234,iPhone 4S,三星Galaxy Note 800 ,三星Galaxy S3,MacBook Air的,Mac Mini的,MacBook Pro的};
    私人的ArrayList<字符串> array_sort =新的ArrayList<字符串>();
    INT长度限制:Textlength = 0;

    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        LV =(ListView控件)findViewById(R.id.ListView01);
        等=(EditText上)findViewById(R.id.EditText01);
        lv.setAdapter(新ArrayAdapter<字符串>(这一点,
        android.R.layout.simple_list_item_1,listview_array));
        INT X = lv.getHeaderViewsCount();
        et.addTextChangedListener(新TextWatcher()
        {
            公共无效afterTextChanged(编辑S)
            {
                // TextWatcher接口的抽象方法。
            }
            公共无效beforeTextChanged(CharSequence中,
            INT开始,诠释计数,诠释后)
            {
                // TextWatcher接口的抽象方法。
            }
            公共无效onTextChanged(CharSequence中,
            INT开始,诠释之前,诠释计数)
            {
                。=长度限制:Textlength et.getText()长度();
                array_sort.clear();
                的for(int i = 0; I< listview_array.length;我++)
                {
                    如果(与长度限制:Textlength LT = listview_array [I] .length())
                    {
                        如果(et.getText()的toString()。equalsIgnoreCase(
                        (串)
                        listview_array [I] .subSequence(0,
                        长度限制:Textlength)))
                        {
                            array_sort.add(listview_array [I]);
                        }
                    }
                }
                lv.setAdapter(新ArrayAdapter<字符串>
                (MainActivity.this,
                android.R.layout.simple_list_item_1,array_sort));
            }
        });
    }
}
 

I have created one listview in which data comes from a string array which is stored in string.xml.

I have one edittext and I want to search a particular data from my listview . My listview data contains like this 011 Delhi delhi... How can I do this?

Here is my code..

adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, arrayList);

Resources res = getResources();
String[] a = res.getStringArray(R.array.states);
arrayList.add("" + a);
adapter.notifyDataSetChanged();

editText.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before,
                              int count) {
        // TODO Auto-generated method stub
    }

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

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

解决方案

I hope the below code works for you.

public class MainActivity extends Activity {
    private ListView lv;
    private EditText et;
    String listview_array[]={"123","TWO","Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "1234", "iPhone 4S", "Samsung Galaxy Note 800", "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
    private ArrayList<String> array_sort = new ArrayList<String>();
    int textlength = 0;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.ListView01);
        et = (EditText) findViewById(R.id.EditText01);
        lv.setAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, listview_array));
        int x= lv.getHeaderViewsCount ();
        et.addTextChangedListener(new TextWatcher()
        {
            public void afterTextChanged(Editable s)
            {
                // Abstract Method of TextWatcher Interface.
            }
            public void beforeTextChanged(CharSequence s,
            int start, int count, int after)
            {
                // Abstract Method of TextWatcher Interface.
            }
            public void onTextChanged(CharSequence s,
            int start, int before, int count)
            {
                textlength = et.getText().length();
                array_sort.clear();
                for (int i = 0; i < listview_array.length; i++)
                {
                    if (textlength <= listview_array[i].length())
                    {
                        if (et.getText().toString().equalsIgnoreCase(
                        (String)
                        listview_array[i].subSequence(0,
                        textlength)))
                        {
                            array_sort.add(listview_array[i]);
                        }
                    }
                }
                lv.setAdapter(new ArrayAdapter<String>
                (MainActivity.this,
                android.R.layout.simple_list_item_1, array_sort));
            }
        });
    }
}

这篇关于如何从一个ListView使用textwatcher搜索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆