Searching the Youtube Playlist in android app -


i followed this sample show youtube videos in android app stuck @ implementing search bar in it. how can implement logic of search bar? tried implement searchable interface failed @ setup part.

this adapter:

public class playlistcardadapter extends  recyclerview.adapter<playlistcardadapter.viewholder> {    .......  public static class viewholder extends recyclerview.viewholder {   public playlistcardadapter(playlistvideos playlistvideos, youtuberecyclerviewfragment.lastitemreachedlistener lastitemreachedlistener) {     mplaylistvideos = playlistvideos;     mlistener = lastitemreachedlistener; }  // create new views (invoked layout manager) @override public playlistcardadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) {     // inflate card layout     view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.youtube_video_card, parent, false);     // populate viewholder     viewholder vh = new viewholder(v);     return vh; }  // replace contents of view (invoked layout manager) @override public void onbindviewholder(final viewholder holder, final int position) {     if (mplaylistvideos.size() == 0) {         return;     }      final video video = mplaylistvideos.get(position);     final videosnippet videosnippet = video.getsnippet();      holder.mtitletext.settext(videosnippet.gettitle());      if (mlistener != null) {         // next playlist page if we're @ end of current page , have page         final string nextpagetoken = mplaylistvideos.getnextpagetoken();         if (!isempty(nextpagetoken) && position == mplaylistvideos.size() - 1) {             holder.itemview.post(new runnable() {                 @override                 public void run() {                     mlistener.onlastitem(position, nextpagetoken);                 }             });         }     } }  @override public int getitemcount() {     return mplaylistvideos.size(); }  private boolean isempty(string s) {     if (s == null || s.length() == 0) {         return true;     }     return false; } 

this link fragment class - link

this solutions consists of having 2 copies of playlist in adapter. 1 complete original list passed adapter, , other list have items result selected applying search.

the second list 1 updated when user submits search, , 1 shown in recycler view.

in following example:

xxxxx playlist item in case.

originaldata keeps complete list of items.

setdata() called update recyclerview new list of items.

setsearchtext() called pass in text filter recycler view items.
can called complete text search, or each time user releases key when typing edittext example.

refreshdata() calls applysearch() filtering , updates recyclerview on screen found items.

public class xxxxxsrecycleradapter extends recyclerview.adapter<xxxxxsrecycleradapter.viewholder> {      private arraylist<xxxxx> data;     private arraylist<xxxxx> originaldata;     private context context;     private string searchtext;      public xxxxxsrecycleradapter(context context, arraylist<xxxxx> data) {         super();         this.context = context;         this.data = data;         this.originaldata = new arraylist<>();         if(this.data != null) {             this.originaldata.addall(this.data);         }     }      public void setdata(arraylist<xxxxx> data) {         this.data = data;         this.originaldata.clear();         if (data != null) {             this.originaldata.addall(this.data);         }         refreshdata();     }      public void setsearchtext(string searchtext){         this.searchtext = searchtext;         refreshdata();     }       private void applysearch(){         if(data != null) {             if (searchtext != null && !searchtext.equals("")) {                 arraylist<xxxxx> tmp = new arraylist<>();                 tmp.addall(data);                 data.clear();                 (xxxxx xxxxx : tmp) {                     if (xxxxx.getnombre().touppercase().contains(searchtext.touppercase())) {                         data.add(xxxxx);                     }                 }             }         }     }      private void refreshdata(){         this.applysearch();         this.notifydatasetchanged();     }      ...  } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -