Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding arguments to AndroidAnnotation injected beans

I have a piece of code using AndroidAnnotations which is very similar to the one found at:

https://github.com/excilys/androidannotations/wiki/Adapters-and-lists

However - I want to pass an argument to the List adapter to specify which list - i.e.

@AfterInject
void initAdapter() {
    persons = personFinder.findAll(companyName);
}

What is the best way to associate companyName with the Adapter? I can't use the constructor with AnroidAnnotations - and @AfterViews is called before the @AfterViews of the parent fragment, so I can't call setters then.

I have currently hacked in a call to set the params manually then refresh the view and removed the @AfterViews - but its nasty and unreliable as I duplicate the pattern down the hierarchy.

EDIT

Just calling the setter works in the most simple case - and is what I currently have. But doesn't work well in the more complicated case. i.e

EFragment->EViewGroup->EBean ListAdapter

Since I can't use the constructor, I have to wait until the full hierarchy is rendered and laid out before the fragment tells the ViewGroup which Company to show company info, which in turn tells the ListAdapter which company so I can get which people, etc. It doesn't take much effort for it to get very messy and if my data was on the web - the UI would probably render like a webpage from the 90s.

I was hoping to use something like @Extras - or have a way to pass arguments for @AfterInject to use, or even just put the companyId in the Fragment Context without tying my ListAdapter to only work with one type of Fragment...

like image 933
Silver Avatar asked Jul 22 '13 23:07

Silver


1 Answers

Try this

@EFragment
public class MyFragment extends Fragment {

  @FragmentArg("myStringArgument")
  String myMessage;

  @FragmentArg
  String anotherStringArgument;

  @FragmentArg("myDateExtra")
  Date myDateArgumentWithDefaultValue = new Date();

}

Source:

https://github.com/excilys/androidannotations/wiki/FragmentArg

like image 172
Cícero Moura Avatar answered Oct 16 '22 16:10

Cícero Moura