Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Android Annotations in custom dialog class

I'm using android annotations, I'm trying to annotate this class so that I can save a value into my shared preferences (annotated) class using @pref. I've managed to find a work around with an intent and a broadcast receiver however this is not ideal and now that I want to fetch a value from the shared preferences in this class to show as the default item selected in the spinner it's starting to leave a smell on my code.

Is there any way to annotate this class?

public class SelectNewsFeedDialog extends Dialog {

    private Context context;
    private Button confirmButton;
    private Spinner spinnerTeams;

    public SelectNewsFeedDialog(final Context context, ArrayList<Team> listTeams) {
        super(context,R.style.cust_dialog);
        this.context = context;
        setContentView(R.layout.dialog_choose_news_feed);
        spinnerTeams = (Spinner) findViewById(R.id.dialog_news_feed_spinner_teams);
        confirmButton = (Button) findViewById(R.id.dialog_news_feed_button_confirm);

        confirmButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Team team = (Team)spinnerTeams.getSelectedItem();
                Intent intent = new Intent(context, IntentCenter_.class);
                intent.putExtra(context.getString(R.string.extra_update_team_news_feed), team.url.toString());
                intent.setAction(context.getString(R.string.action_update_team_news_feed));
                context.sendBroadcast(intent);
                dismiss();
            }
        });
        SpinnerTeamsAdapter adapter = new SpinnerTeamsAdapter(context, listTeams);
        spinnerTeams.setAdapter(adapter);       
    }
}
like image 451
Mike Bryant Avatar asked Aug 17 '13 14:08

Mike Bryant


1 Answers

Currently, we haven't any annotation for Dialog classes. You may want to uses @EBean on this but the compiler is yelling on missing constructors.

The solution is to uses a DialogFragment instead of a Dialog and annotate this class with @EFragment. The following code should works :

@EFragment(R.layout.dialog_choose_news_feed)
public class SelectNewsFeedDialog extends DialogFragment {

    @ViewById
    Button confirmButton;

    @ViewById
    Spinner spinnerTeams;

    @Extra
    List<Team> listTeams;

    @Click
    public void confirmButtonClicked() {
        Team team = (Team) spinnerTeams.getSelectedItem();
        Intent intent = new Intent(context, IntentCenter_.class);
        intent.putExtra(context.getString(R.string.extra_update_team_news_feed), team.url.toString());
        intent.setAction(context.getString(R.string.action_update_team_news_feed));
        context.sendBroadcast(intent);
        dismiss();
    }

    @AfterViews
    public void init() {
        SpinnerTeamsAdapter adapter = new SpinnerTeamsAdapter(getActivity(), listTeams);
        spinnerTeams.setAdapter(adapter);
    }
}

However, using @Extra on a list is not a good idea. You should : * use a list of ids annotated with @Extra * or, uses a setter and passes this list to your adapter after the dialog was been initialized.

Hope this helps

like image 130
DayS Avatar answered Sep 22 '22 19:09

DayS