android - Problem displaying DialogFragment from button in Adapter Android
问题描述
I want to display a DialogFragment from my adapter button. I have created a DialogFragment class inside the adapter itself and I want it to show me the dialog when I press the button, but I get this error:
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)
This is my adapter:
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" );
}
});
}
}
And this is the reference in the Fragment:
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 是您调用适配器的类。
FragmentManager fm = activity.getSupportFragmentManager();
DialogFragment newFragment = new BidKaratekaDialogFragment();
newFragment.show(fm, "bid" );
}
});
智者说。。
一个上下文并不总是一个活动,即使你在一个活动中。它可以是一个应用程序,也可以是另一个上下文的包装器。将 Context 强制转换为 Activity 几乎总是错误的。如果你绝对需要,你应该传入一个 Activity 作为参数,而不是一个 Context。或者更好的是,直接传递支持片段管理器而不是活动,因为这就是你所需要的。
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, ....)
推荐阅读
- php - 什么决定了服务器或 LAMP 堆栈的最大并发用户数?
- angular - 如何使用量角器检查小吃店的可见性并单击 e2e 测试中的操作?
- android - 安卓应用程序调用之间的安全和限制
- python - BeautifulSoup4:通过 find() 无法找到具有特定 href 值的“a”标签
- pine-script - 将 Amibroker 脚本转换为 pine 脚本
- java - MainActivity 没有从 Android Studio App 中的服务类接收数据
- php - Laravel Faker 工厂关系
- javascript - 用 JS 递归转换我的 JSON 数据
- firebase-authentication - 如何在 Firebase Cloud Functions 中获取 ID 令牌
- linux - 如何以正确的方式将多个文件添加到 .gitignore 文件中?