java - How to create an interface to get info from a Fragment to an Android Activity? -
over past days i've desperately been trying build android app simple fragment (which use twice). want pass contents of fragments' edittext-boxes new activity. can't figure out how contents fragments. have far this:
i've got edit_text_fragment.xml
:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <edittext android:id="@+id/my_edit_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="my hint" /> </linearlayout>
and corresponding myedittextfragment.java
:
public class myedittextfragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.edit_text_fragment, container, false); return view; } }
i use fragment twice in main.xml
this:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <fragment android:id="@+id/detailfragment_placeholder" android:layout_width="fill_parent" android:layout_height="wrap_content" class="com.example.fragmenttester5.myedittextfragment" /> <fragment android:id="@+id/detailfragment_placeholder2" android:layout_width="fill_parent" android:layout_height="wrap_content" class="com.example.fragmenttester5.myedittextfragment" /> <button android:id="@+id/submit_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="submit of it" /> </linearlayout>
and in mainactivity hooked button new activity:
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button submitbutton = (button) findviewbyid(r.id.submit_button); submitbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v){ intent intent = new intent(mainactivity.this, otheractivity.class); intent.putextra("result1", "the_result_from_the_first_edittext"); intent.putextra("result2", "the_result_from_the_second_edittext"); startactivity(intent); } }); } }
i think need define kind of interface in fragment, can't find how. read couple examples , tutorials (like this one), make no sense me @ all. don't understand code given , don't understand how adjust use case.
so question; can me contents of fragment within activity? examples very welcome since i'm banging head against wall here..
so you'd create interface talk activity
if event happened in fragment
. situations this, can make accessible method in fragment
activity
can call. so
public class myedittextfragment extends fragment { private edittext medittext; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.edit_text_fragment, container, false); return view; } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); medittext = (edittext) getview().findviewbyid(r.id.my_edit_text); } public editable gettext() { return medittext.gettext(); } }
then
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final myedittextfragment fragment1 = (myedittextfragment) getfragmentmanager().findfragmentbyid(r.id.detailfragment_placeholder); final myedittextfragment fragment2 = (myedittextfragment) getfragmentmanager().findfragmentbyid(r.id.detailfragment_placeholder2); button submitbutton = (button) findviewbyid(r.id.submit_button); submitbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v){ string firstresult = fragment1.gettext().tostring(); string secondresult = fragment2.gettext().tostring(); intent intent = new intent(mainactivity.this, otheractivity.class); intent.putextra("result1", firstresult); intent.putextra("result2", secondresult); startactivity(intent); } }); } }
this assumes assigned fragment
tags in fragmenttransaction
. sure check null fragments
(omitted brevity)
Comments
Post a Comment