【问题标题】:Problem displaying DialogFragment from button in Adapter Android从适配器 Android 中的按钮显示 DialogFragment 时出现问题
【发布时间】:2020-08-13 10:51:52
【问题描述】:

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

java.lang.ClassCastException: android.app.Application cannot be cast to androidx.appcompat.app.AppCompatActivity
        at com.example.karate_manager.Adapters.AdapterMarket$1.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);

【问题讨论】:

  • FragmentActivity 活动 = (FragmentActivity)(activity1); ,什么是activity1?
  • 对不起,我更新了,我尝试了几个代码,但它仍然不起作用......
  • 跳之前需要了解

标签: android listview android-fragments android-arrayadapter


【解决方案1】:
               public void onClick(View view) {
                Application activity1 = (Application) view.getContext(); // bad

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

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

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

智者说..

上下文并不总是一个活动,即使您在活动中也是如此。 它可以是一个应用程序,也可以是另一个上下文的包装器。它是 将 Context 转换为 Activity 几乎总是错误的。如果你 绝对需要,你应该传入一个Activity作为参数, 而不是上下文。或者更好的是,传入支持片段 经理直接而不是活动,因为这就是你所需要的 它为。

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

【讨论】:

  • 我不明白,抱歉。在这种情况下我必须如何传递上下文?我像这样更改代码FragmentActivity activity = (FragmentActivity)(context); 。但我有同样的错误。
  • 你从哪个类调用适配器,传递那个类的引用,
  • 从片段中,我在 ListView 中显示适配器。你是这个意思吗?
  • 然后,从您调用的位置初始化 FragmentManager,并在此处传递引用。然后初始化对话框并显示
  • 你知道如何将 FragmentManage 的引用从我的片段传递给适配器吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
相关资源
最近更新 更多