Adding arguments to AndroidAnnotation injected beans
Asked Answered
S

1

6

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...

Squirmy answered 22/7, 2013 at 23:43 Comment(2)
i can't understand why a setter for the companyName in the adapter is not enough. I think that a simple setter of persons with notifyDataSetChanged() is the simplest way.Wellmannered
Certainly works in the most simple case - and is what I currently have.Squirmy
A
2

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

Agrapha answered 7/10, 2014 at 19:57 Comment(2)
Don't think this was released when I asked the question... But it the answer. I think.Squirmy
What if the class we want to add parameters to isn't a fragment but a regular bean?Greegree

© 2022 - 2024 — McMap. All rights reserved.