Android Transform the current Fragment Class to Activity Class -
i have fragment somethings related searchview works well,
but want create activity perform tasks typically fragment.
there no problem xmls main problem in java code.some methods in fragment , don't know opposite of them in activity.
fragment code:
public class airlinesearchfragment extends fragment implements searchview.onquerytextlistener{ private recyclerview recyclerview; private list<airlinedata> airlinesdatamodel; private airlinessearchviewadapter adapter; public airlinesearchfragment() { sethasoptionsmenu(true); } @override public void oncreate(bundle savedinstancestate) { sethasoptionsmenu(true); super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { sethasoptionsmenu(true); view view = inflater.inflate(r.layout.airline_search_fragment, container, false); recyclerview = (recyclerview) view.findviewbyid(r.id.recyclerview); linearlayoutmanager layoutmanager = new linearlayoutmanager(getactivity()); recyclerview.setlayoutmanager(layoutmanager); return view; } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); sethasoptionsmenu(true); string[] locales = locale.getisocountries(); airlinesdatamodel = new arraylist<>(); airlinesdatamodel.addall(airlinescore.airlineslist); adapter = new airlinessearchviewadapter(airlinesdatamodel); recyclerview.setadapter(adapter); } @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { inflater.inflate(r.menu.auto_complete, menu); final menuitem item = menu.finditem(r.id.action_search); final searchview searchview = (searchview) menuitemcompat.getactionview(item); searchview.setonquerytextlistener(this); menuitemcompat.setonactionexpandlistener(item, new menuitemcompat.onactionexpandlistener() { @override public boolean onmenuitemactioncollapse(menuitem item) { // when collapsed adapter.setfilter(airlinesdatamodel); return true; // return true collapse action view } @override public boolean onmenuitemactionexpand(menuitem item) { // when expanded return true; // return true expand action view } }); } @override public boolean onquerytextchange(string newtext) { final list<airlinedata> filteredmodellist = filter(airlinesdatamodel, newtext); adapter.setfilter(filteredmodellist); return true; } @override public boolean onquerytextsubmit(string query) { return false; } private list<airlinedata> filter(list<airlinedata> models, string query) { query = query.tolowercase(); final list<airlinedata> filteredmodellist = new arraylist<>(); (airlinedata model : models) { if(model.arabicname==null) { model.arabicname=""; } if(model.shortname==null) { model.shortname=""; } if(model.name==null) { model.name=""; } final string text = model.name.tolowercase()+model.shortname.tolowercase() +model.arabicname.tolowercase()+model.code.tolowercase(); if (text.contains(query)) { filteredmodellist.add(model); } } return filteredmodellist; } }
my activity methods copied fragment(what should add it?):
public class airlinessearchactivity extends appcompatactivity implements searchview.onquerytextlistener{ private recyclerview recyclerview; private list<airlinedata> airlinesdatamodel; private airlinessearchviewadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_airlines_search); } @override public boolean onquerytextchange(string newtext) { final list<airlinedata> filteredmodellist = filter(airlinesdatamodel, newtext); adapter.setfilter(filteredmodellist); return true; } @override public boolean onquerytextsubmit(string query) { return false; } private list<airlinedata> filter(list<airlinedata> models, string query) { query = query.tolowercase(); final list<airlinedata> filteredmodellist = new arraylist<>(); (airlinedata model : models) { if(model.arabicname==null) { model.arabicname=""; } if(model.shortname==null) { model.shortname=""; } if(model.name==null) { model.name=""; } final string text = model.name.tolowercase()+model.shortname.tolowercase() +model.arabicname.tolowercase()+model.code.tolowercase(); if (text.contains(query)) { filteredmodellist.add(model); } } return filteredmodellist; } }
try moving logic in oncreateview
, onviewcreated
activity.oncreate()
.
then override oncreateoptionsmenu
in activity , copy logic had in fragment
Comments
Post a Comment