从适配器中的按钮显示 DialogFragment 时出现问题 Android

Problem displaying DialogFragment from button in Adapter Android

我想从我的适配器按钮显示一个 DialogFragment。我在适配器本身内部创建了一个 DialogFragment class,我希望它在我按下按钮时向我显示对话框,但出现此错误:

java.lang.ClassCastException: android.app.Application cannot be cast to androidx.appcompat.app.AppCompatActivity
        at com.example.karate_manager.Adapters.AdapterMarket.onClick(AdapterMarket.java:106)

这是我的适配器:

public class AdapterMarket extends ArrayAdapter{
private FragmentManager fm;
    Context context;
    int item_Layaut;
    ArrayList<Karateka> data;
    ApiUtils apiUtils;

  public AdapterMarket(@NonNull Context context, int resource, @NonNull ArrayList objects, FragmentManager fm) {
        super(context, resource, objects);
        this.context = context;
        this.item_Layaut = resource;
        this.data = objects;
        this.fm = fm;
    }

    public void setData(MarketResponse data) {
        if(data!=null){
            this.data = data.getKaratekas();
            this.notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(item_Layaut, parent, false);
        }

        String value = String.valueOf(data.get(position).getValue());
        Button buttonValue = convertView.findViewById(R.id.item_button_value_karateka);
        buttonValue.setText(value);

        popupBidKarateka(buttonValue);

        return convertView;
    }

    public void popupBidKarateka(Button buttonValue){
        buttonValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentActivity activity = (FragmentActivity)(context);
                fm = activity.getSupportFragmentManager();
                DialogFragment newFragment = new BidKaratekaDialogFragment();
                newFragment.show(fm, "bid" );
            }
        });
    }
}

这是片段中的引用:

     FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        adapterMarket = new AdapterMarket(getActivity().getApplicationContext(), R.layout.item_market_layout, marketResponse.getKaratekas(), fragmentManager);

               public void onClick(View view) {
                Application activity1 = (Application) view.getContext(); // bad

                FragmentActivity activity = (FragmentActivity)(context); // context must come from FragmentActivity

// 我假设 FragmentActivity 是您调用适配器的 class。

                FragmentManager fm = activity.getSupportFragmentManager();
                DialogFragment newFragment = new BidKaratekaDialogFragment();
                newFragment.show(fm, "bid" );
            }
        });

智者说..

A context is not always an Activity, even when you're in an Activity. It could be an Application, or a wrapper around another context. It's almost always wrong to cast a Context to an Activity. If you absolutely need one, you should pass in an Activity as a parameter, rather than a Context. Or better yet, pass in the support fragment manager directly rather than the activity, since that's all you need it for.

public AdapterMarket(@NonNull Context context, FragmentManager fm, int resource, @NonNull ArrayList objects) {
        super(context, resource, objects);
        this.context = context;
        this.fm = fm;
        this.item_Layaut = resource;
        this.data = objects;
    }

// 说,Afragment 正在调用适配器。

AFragment.class
FM fm = getActivity.getSupportFM/getFM();
AdapterMarket market = new AdapterMarket(someContext, fm, ....)