Monday, 15 April 2013

android - Passing value from inner fragment to outer fragment -



android - Passing value from inner fragment to outer fragment -

i working on creating form fragment(layout) in activity.in form have input fields utilize select date , time.i populating date , time using datepicker dialog fragment seperate fragment.

http://developer.android.com/guide/topics/ui/controls/pickers.html

i facing problem how update input field in form fragment datepicker fragment.i did seek approach null value.

http://developer.android.com/training/basics/fragments/communicating.html

also above method right approach or should add together date picker fragment inner class in form fragment connsidering may not utilize datepicker elsewhere.

ps: noob.any help appreciated.

solution:

since using fragmentpageradapter, in inner fragment generated list of fragments using this

list<fragment> fragmentlist = this.getfragmentmanager().getfragments();

created singleton class , added method /getting form fragment/

public fragment getmyeventfragment(list<fragment> fragmentlist){ fragment myeventfragment =null; (int = 0; < fragmentlist.size(); i++) { string currenttag = fragmentlist.get(i).gettag(); string lastchar = currenttag.substring(currenttag.length() - 1); system.out.println("the lastchar " + lastchar); if (lastchar.equalsignorecase("1")) { myeventfragment = fragmentlist.get(i); break; } } homecoming myeventfragment; }

since form fragment in sec tab in fragmentpageradapater value 1 lastly element in autogenerated fragment tag refers fragment position.

once obtained form fragment update corresponding input field values.

set tag each fragment enable phone call them

how set fragment tag , can add together tag fragment while transaction

// getsupportfragmentmanager() if utilize back upwards library // tag optional , should assign 1 phone call 1 time again getfragmentmanager().begintransaction().add(android.r.id.content, fragment, "your_tag").commit(); // can phone call want android.app.fragmentmanager fragmentmanager = getfragmentmanager(); firstfragment fragment = (firstfragment) fragmentmanager.findfragmentbytag( "your_tag" ); // utilize fragment ever want fragment.update(string);

there anther illustration how determine fragment time id

<framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment class="com.example.android.apis.app.fragmentlayout$titlesfragment" android:id="@+id/titles" android:layout_width="match_parent" android:layout_height="match_parent" /> </framelayout>

fragment id here titles android:id="@+id/titles"

when want phone call it

titlesfragment titles= (titlesfragment) getsupportfragmentmanager().findfragmentbyid(r.id.titles);

this works if fragment identifier in xml

android android-fragments

No comments:

Post a Comment