Use Android Annotations in custom dialog class
Asked Answered
P

1

7

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);       
    }
}
Pelite answered 17/8, 2013 at 14:51 Comment(0)
G
11

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

Guaranty answered 21/8, 2013 at 8:36 Comment(2)
Thanks I will try to implement this tonight. the extra is just an item of the list, and was only necessary because I needed to use the annotated sharedPreferences. so I sent an intent and then picked it up in the annotated class, not a great way of doing it by any means but it worksPelite
DialogFragment has an issue if the support library version is previous than 22.2.1, so update the library and the issue should disappear. It is important to remember that when you using DialogFragment the activity/fragment lifecycle wont be affected when the dialog is dismissed, because since it is indeed a fragment the activity is currently in the foreground, so onResume won't be called, you should create an interface that triggers an event in the dialog's onDismiss method. Thanks for the help!Memento

© 2022 - 2024 — McMap. All rights reserved.