LMK
LMK

Reputation: 2952

Custom Filtering in listview android

I am a new to Android,I have a Listview with some items, each item contains string of size 20 characters(1st character to 16th character are same).I have to add search option to list view in such a way that,depending on last 4 characters only the list should filter. I have seen many sites but i couldn't find.

ex:
list items : 123456789ABCD1
list items : 123456789EFGH2
list items : 123456789IJKL3
list items : 123456789MNOP4

if i use

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

}

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

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {




    myAdapter.getFilter().filter(s.toString());

}

i should enter
123456789A in Edittext then only filter occur in listview

My requirement is
i should enter AB in Edittext so that it starts filtering

I would appreciate for your ideas. Thanks in advance

Upvotes: 0

Views: 300

Answers (2)

Aarun
Aarun

Reputation: 564

What i think you have to do is , As you told that the list item has a fixed length (i.e 20 letters )so first of all get the last four letters of the string by subString method. And then you will put the resulting string for your searching Query.

geting search string

String Str = new String("123456789ABCD");
        int i = Str.length();
        System.out.print("Return Value :");
        System.out.println(Str.substring(10));

        System.out.print("Return Value :");
        System.out.println(Str.substring(i - 4, i));

String search_str=Str.substring(i - 4, i);

put "search_str" into SQL Query

SELECT * FROM table_name WHERE code LIKE '%search_str%'

Upvotes: 0

AAnkit
AAnkit

Reputation: 27549

You can do it in two ways.

1) apply filter on the array data you have in list vie , Use String.Contains() method for this purpose.

Example :- ListArray[i].toString.contains(SearchString); // it will return true if search string is present.

2)If you hold list data into Sqlite database. You can run a query like below

Example :-sqliteDatabaseObj.Query(TABLE_NAME,null,COLUMN_NAME+" like %"+searchString"%",null,null);

here is another example of this Example of list data filtering

P.S:- Examples i written are not syntactically correct. Please check the docs and use them,

Upvotes: 1

Related Questions