android - The interaction fragments -
i have 1 activity , 2 fragments. in drawer menu have 2 items: fragment1 , fragment2. in fragment1 have edittext , button. when click on button - text field must recorded in listview of second fragment. please me, how can make this.
i changed code, when click on button application has stopped. please me.
mainactivity
public class mainactivity extends appcompatactivity implements screenone.onfragmentinteractionlistener { drawer drawerresult; arraylist<string> arrayitems; string message; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); primarydraweritem item1 = new primarydraweritem() .withname(r.string.navigation_menu_item1) .withidentifier(1); primarydraweritem item2 = new primarydraweritem() .withname(r.string.navigation_menu_item2) .withidentifier(2); drawerresult = new drawerbuilder() .withactivity(this) .withtoolbar(toolbar) .withactionbardrawertoggleanimated(true) .withheader(r.layout.drawer_header) .adddraweritems(item1) .adddraweritems(item2) .withondraweritemclicklistener(new drawer.ondraweritemclicklistener() { @override public boolean onitemclick(view view, int position, idraweritem draweritem) { fragmentmanager manager = getsupportfragmentmanager(); fragmenttransaction transaction = manager.begintransaction(); fragment fragment = new fragment(); switch (position){ case 1: fragment = new screenone(); break; case 2: fragment = new screentwo(arrayitems); break; } transaction.replace(r.id.content, fragment); transaction.commit(); return false; } }) .build(); } @override public void onbackpressed(){ if(drawerresult.isdraweropen()){ drawerresult.closedrawer(); } else{ super.onbackpressed(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void onfragmentinteraction(string msg) { arrayitems.add(msg); }
}
screenone or fragment1
public class screenone extends fragment { button btnadd; edittext et; private onfragmentinteractionlistener mlistener; arraylist<string> items; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.screen_one_fragment, container, false); btnadd = (button) view.findviewbyid(r.id.btnadd); et = (edittext) view.findviewbyid(r.id.et); btnadd.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string str = et.gettext().tostring(); mlistener.onfragmentinteraction(str); items.add(str); //updatedetail(); } }); return view; } public interface onfragmentinteractionlistener { void onfragmentinteraction(string msg); } @override public void onattach(activity activity) { super.onattach(activity); try { mlistener = (onfragmentinteractionlistener) activity; } catch (classcastexception e) { throw new classcastexception(activity.tostring() + " должен реализовывать интерфейс onfragmentinteractionlistener"); } } public void updatedetail() { // генерируем некоторые данные string newtime = string.valueof(system.currenttimemillis()); // Посылаем данные activity mlistener.onfragmentinteraction(newtime); }
}
screentwo or fragment2
public class screentwo extends fragment { arrayadapter<string> adapter; arraylist<string> itemarray; public screentwo(arraylist<string> itemarray){ this.itemarray = itemarray; } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.screen_two_fragment, container, false); listview listview = (listview) view.findviewbyid(r.id.lvname); itemarray = new arraylist<>(); itemarray.clear(); adapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_1, itemarray); listview.setadapter(adapter); return view; }
my guess add third method in interface onfragmentinteractionlistener
, returns list<string> links
, going list of links, stored screenone
.
when screentwo
attached (in onattach(activity)
callback) activity, retrieve list , build ui based on it.
another thing declare "listener" interface, every time call onfragmentinteraction(string)
, notify listeners changes, occurred.
Comments
Post a Comment