Thursday, 15 July 2010

java - Get input from inactive tab -



java - Get input from inactive tab -

i have 2 tabs in layout. failing input value entered in edittext in inactive tab. lets tab holds registerloginfragment selected. next code returns expected value.

edittext edittextemail = (edittext)registerloginfragment.getview().findviewbyid(r.id.edittextemail);

but getting value 2nd tab inactive fails npe because getview() of inactive tab returns null. next failing:

edittext edittextfirstname = (edittext)registerpersonalfragment.getview().findviewbyid(r.id.edittextfirstname);

so need input both tabs when button on 1st tab clicked. both tabs visited , have values entered.

little more code:

public void ontabselected(tab tab, fragmenttransaction ft) { if(tab.gettag().tostring().equals("login_tab")){ if(registerloginfragment == null){ registerloginfragment = fragment.instantiate(this, registerloginfragment.class.getname()); ft.add(r.id.linearlayoutregister, (fragment)registerloginfragment); } else{ ft.attach((fragment)registerloginfragment); } currentfragment = registerloginfragment; } else if(tab.gettag().tostring().equals("personal_tab")){ if(registerpersonalfragment == null){ registerpersonalfragment = fragment.instantiate(this, registerpersonalfragment.class.getname()); ft.add(r.id.linearlayoutregister, (fragment)registerpersonalfragment); } else{ ft.attach((fragment)registerpersonalfragment); } currentfragment = registerpersonalfragment; } }

so not right way values inactive tab failing find proper way.

i guess can this: get text edit text fields in multiple tabs

but there more "elegant" way?

thanks velja

i found funny way achieving task. instead of detaching , attaching (or using fragmenttransaction.replace) showing , hiding fragments on tab actions. note of import utilize gone value , not invisible since gone doesn't utilize space in view: http://developer.android.com/reference/android/view/view.html#gone leave question open hear if there still more "proper" way? refactored code looks this:

@override public void ontabselected(tab tab, fragmenttransaction ft) { if(tab.gettag().tostring().equals("login_tab")){ if(registerloginfragment == null){ registerloginfragment = fragment.instantiate(this, registerloginfragment.class.getname()); ft.add(r.id.linearlayoutregister, (fragment)registerloginfragment); } else{ //ft.attach((fragment)registerloginfragment); registerloginfragment.getview().setvisibility(view.visible); } currentfragment = registerloginfragment; } else if(tab.gettag().tostring().equals("personal_tab")){ if(registerpersonalfragment == null){ registerpersonalfragment = fragment.instantiate(this, registerpersonalfragment.class.getname()); ft.add(r.id.linearlayoutregister, (fragment)registerpersonalfragment); } else{ //ft.attach((fragment)registerpersonalfragment); registerpersonalfragment.getview().setvisibility(view.visible); } currentfragment = registerpersonalfragment; } } @override public void ontabunselected(tab tab, fragmenttransaction ft) { if(currentfragment != null){ //ft.detach((fragment)currentfragment); currentfragment.getview().setvisibility(view.gone); } }

java android

No comments:

Post a Comment