android - Filtering a ListFragment from the ActionBar in parent Activity -


i'm hoping can me this.

i have mainactivity class holds listfragment, , i'm trying build search bar action bar in mainactivity.

the search bar there, don't know how filter list in listfragment.

there's filterlist method in listfragment going call mainactivity, i'm not sure if correct.

here's mainactivity looks like:

package dk.mmad.ma1;  public class mainactivity extends fragmentactivity { private dao daoadapter; private simplecursoradapter cursoradapter;  fragment fragment;  public dao getdao() {     return daoadapter; }  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      daoadapter = new dao(this);      getactionbar().setdisplayuselogoenabled(false);     getactionbar().setdisplayshowtitleenabled(false);     getactionbar().setdisplayshowhomeenabled(false);      fragmentmanager fm = getsupportfragmentmanager();     fragment = fm.findfragmentbyid(r.id.fragmentcontainer);      if (fragment == null) {         fragment = new booklistfragment();         fm.begintransaction()             .add(r.id.fragmentcontainer, fragment).commit();     } }  /**  * on selecting action bar icons  * */ @override public boolean onoptionsitemselected(menuitem item) {     // take appropriate action each action item click     switch (item.getitemid()) {     case r.id.action_search:         // filter list         return true;     case r.id.add_book:         intent intent = new intent(mainactivity.this, addbookactivity.class);         startactivityforresult(intent, 1);         return true;     default:         return super.onoptionsitemselected(item);     } }  @override public void ondestroy() {     super.ondestroy();     daoadapter.close(); }  @override public boolean oncreateoptionsmenu(menu menu) {     menuinflater inflater = getmenuinflater();     inflater.inflate(r.menu.activity_main_actions, menu);    // associate searchable configuration searchview     searchmanager searchmanager = (searchmanager) getsystemservice(context.search_service);     searchview searchview = (searchview) menu.finditem(r.id.action_search)             .getactionview();     searchview.setsearchableinfo(searchmanager             .getsearchableinfo(getcomponentname()));        return super.oncreateoptionsmenu(menu); }  } 

and here's listfragment:

    package dk.mmad.ma1;  import android.annotation.suppresslint; import android.content.intent; import android.database.cursor; import android.os.bundle; import android.support.v4.app.listfragment; import android.util.log; import android.view.view; import android.widget.listview; import android.widget.simplecursoradapter; import db.dao; import db.dao.books;  @suppresslint("newapi") public class booklistfragment extends listfragment {      private dao daoadapter;     private simplecursoradapter cursoradapter;     long currentid;      public void onresume() {         super.onresume();         refreshlist();     }      public void filterlist(string query) {         cursor cursor = daoadapter.getbooks(query);         cursoradapter.changecursor(cursor);     }      private void refreshlist() {         cursor cursor = daoadapter.getbooks();         cursoradapter.changecursor(cursor);     }      public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          daoadapter = ((mainactivity)getactivity()).getdao();         daoadapter.open();          daoadapter.savebook("testbook1", "author1", 123);         daoadapter.savebook("testbook2", "author3", 123);         daoadapter.savebook("testbook3", "author1", 123);         daoadapter.savebook("testbook4", "author3", 123);         daoadapter.savebook("testbook5", "author1", 123);         daoadapter.savebook("testbook6", "author3", 123);         daoadapter.savebook("testbook7", "author1", 123);         daoadapter.savebook("testbook8", "author3", 123);         daoadapter.savebook("testbook9", "author1", 123);         daoadapter.savebook("testbook10", "author3", 123);         daoadapter.savebook("testbook11", "author1", 123);         daoadapter.savebook("testbook13", "author3", 123);         daoadapter.savebook("testbook14", "author1", 123);         daoadapter.savebook("testbook15", "author3", 123);           cursor cursor = daoadapter.getbooks();         string[] fromcolumns = { "title", "author" };         int[] toviews = { dk.mmad.ma1.r.id.titletextview, dk.mmad.ma1.r.id.authortextview };          cursoradapter = new simplecursoradapter(((mainactivity)getactivity()),                 dk.mmad.ma1.r.layout.list_item_books, cursor, fromcolumns,                 toviews, 0);          setlistadapter(cursoradapter);         //startmanagingcursor(cursor);     }  /*  @override     public void onlistitemclick(listview l, view v, int position, long id) {                 long itemid = getlistadapter().getitemid(position);          log.i("testings", daoadapter.getbooktitle(itemid));           //intent intent = new intent(getactivity(), bookdetailsactivity.class);         //intent.putextra("", itemid);         //startactivity(intent);     }*/      @override     public void onlistitemclick(listview l, view v, int position, long id) {         super.onlistitemclick(l, v, position, id);         currentid = id;          cursor cursor = cursoradapter.getcursor();         string idforintent = cursor.getstring(cursor.getcolumnindexorthrow(books.id_col));         string title = cursor.getstring(cursor.getcolumnindexorthrow("title"));         string author = cursor.getstring(cursor.getcolumnindexorthrow("author"));         string pages = cursor.getstring(cursor.getcolumnindexorthrow("page_count"));          intent intent = new intent(getactivity().getapplicationcontext(), bookdetailsactivity.class);         intent.putextra("book", new string[] {idforintent, title, author, pages});         startactivity(intent);     }  } 

can me?


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -