Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

android - How to search data from a listview using textwatcher?

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
    }
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...