Friday, 15 June 2012

android - search functionality for json content -



android - search functionality for json content -

i developed 1 android app content fetched json done using asynctask , populated in listviews. have 10 custom list views in app. want implement single search in home page. new android. please suggest me, how implement it. search needed titles node in json object. knew how set search widget, listview xml, adding manifest file metadata, , search activity main activity. want know, how implement single search custom list views.

my json file looks below. custom listview contains image , title , description.

[ { "name":"steve", "title":"how things work", "image":url, }, { "name":"jack", "date":"helium extraction", "image":url, } ]

1. first , create object user example:

public class user{ string name; string title; string urlimage; // setter // getter }

2. create listadapter extends baseadapter populate info in listview

(look @ tutoruel androidhive)

also see this tutoriel (in french) tutozone.info see how populate info in custiom adapter

3. search fonctionality ,create layout layout_search contains edittext searchedittext

4. include layout on each layout contains listview info (from json)

5. in asyncktask, add together each jsonobject arraylist<user> , @ end of parsing have arraylist contains of info [user(name, title,url)] , populated in listview

6. bvack search fonctionality , on each activity add together listener edittext: addtextchangedlistener:

edittext fillsearch=(edittext)findviewbyid(r.id.searchedittext); fillsearch.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence arg0, int arg1, int arg2, int arg3) { // 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 aftertextchanged(editable arg0) { // todo auto-generated method stub // todo auto-generated method stub string text = fillsearch.gettext().tostring().tolowercase(locale.getdefault()); arraylist<user> list = search(text); // our function } });

7. add together search method homecoming list contains info ythat search (starts text entered user in edittext) :

public arraylist<user>search(string s) { arraylist<user> list= new arraylist<user>(); (user u: listeusers) { // listusers= contains info parsed in asyncktask if(user.gettitle().tolowercase().startswith(s)) { list.add(u); } } homecoming list; }

android json search

No comments:

Post a Comment