Daniëlle
Daniëlle

Reputation: 95

How to call getSupportFragmentManager() from adapter class (which doesn't extend AppCompatActivity)?

I want to make a dialog that pops up if you click on a Name TextView in my ListView.

So I've been following this youtube tutorial: https://www.youtube.com/watch?v=ARezg1D9Zd0&t=511s and he makes a dialog that pops up from his MainActivity. But I want to pop it up from my ListView, so from my Adapter.

Because of that I can't call getSupportFragmentManager(). I think it's because my Adapter does extend ArrayAdapter<> instead of AppCompatActivity. Can anyone help me?

It goes wrong when I say: changeNameDialog.show(getSupportFragmetnManager(), "Change Name Dialog");

public class PlayersAdapter extends ArrayAdapter<Player> {
    Tournament tournament = new Tournament();

    private LayoutInflater Inflater;

    public PlayersAdapter(@NonNull Context context, @NonNull List<Player> objects) {
        super(context, R.layout.list_players,objects);

        Inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = Inflater.inflate(R.layout.list_players, parent, false);
        }

        ImageView profilePictureButtonImageView = convertView.findViewById(R.id.profilePictureButtonImageView);
        TextView namePlayerTextView = convertView.findViewById(R.id.namePlayerTextView);
        TextView pointsTextView = convertView.findViewById(R.id.pointsTextView);
        ImageView deleteButtonImageView = convertView.findViewById(R.id.deleteButtonImageView);

        Player player = getItem(position);

        profilePictureButtonImageView.setImageResource(player.getProfilePicture());
        namePlayerTextView.setText(player.getName());
        pointsTextView.setText(tournament.getPlayers().get(position).getTotalPoints() + "");

        // Change name
        namePlayerTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChangeNameDialog changeNameDialog = new ChangeNameDialog();
                changeNameDialog.show(getSupportFragmetnManager(), "Change Name Dialog");
            }
        });

        return convertView;
    }
}

Upvotes: 0

Views: 104

Answers (1)

Shahood ul Hassan
Shahood ul Hassan

Reputation: 789

Create an interface with a method in your Adapter class as follows:

public interface OnAdapterInteractionListener {

    void showDialog();
}

Change the constructor of your Adapter class and receive your Activity / Fragment as OnAdapterInteractionListener in it. Save it as a global variable in your Adapter class. While calling this constructor from your Activity or Fragment, pass on this for OnAdapterInteractionListener.

private LayoutInflater Inflater;
private OnAdapterInteractionListener mListener;

public PlayersAdapter(@NonNull Context context, @NonNull List<Player> objects, OnAdapterInteractionListener listener) {
    super(context, R.layout.list_players,objects);

    Inflater = LayoutInflater.from(context);
    mListener = listener;
}

Make your Activity / Fragment implement the adapter interface OnAdapterInteractionListener and implement its method as follows:

@Override
public void showDialog() {
    ChangeNameDialog changeNameDialog = new ChangeNameDialog();
    changeNameDialog.show(getSupportFragmentManager(), "Change Name Dialog");
}

Finally, in your Adapter, amend your onClick() method as follows:

namePlayerTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mListener != null) mListener.showDialog();
        }
    });

Hope it serves your purpose!

Upvotes: 1

Related Questions