gpt4 book ai didi

java - 如何在 RecyclerView 适配器中显示 ImageView onClick 中的 Dialogfragment

转载 作者:行者123 更新时间:2023-12-01 09:26:52 25 4
gpt4 key购买 nike

我有 ActionBarDialog (DialogFragment)、CarAdapter (RecycleView) 和 SportFragment。我在 SportFragment 中创建方法(infoMethod)来显示 DialogFragment 并正常工作。但如果我想在 CarAdapter 中调用 infoMethod 则不起作用(java.lang.NullPointerException)

SportFragment 中的 infoMethod

public void infoMethod(){
Bundle args = new Bundle();
args.putString("title", "Dialog with Action Bar");
ActionBarDialog actionbarDialog = new ActionBarDialog();
actionbarDialog.setArguments(args);
actionbarDialog.show(getActivity().getSupportFragmentManager(),
"action_bar_frag");
}

我尝试从 CarAdapter 调用 infoMethod

        public MyViewHolder(View view) {
super(view);

image= (ImageView) view.findViewById(R.id.imageViewlist);
image.setOnClickListener(this);

}
@Override
public void onClick(View v) {
SportFragment infoFrag = new SportFragment();
if (v.getId() == image.getId()){
v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.imagelist));
infoFrag.infoMethod(); //call infoMethod not work

Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}

日志猫

09-29 13:30:09.292 18661-18661/com.paijostudio.hitungmasa E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.paijostudio.hitungmasa.fragment.SportFragment.infoMethod(SportFragment.java:210)
at com.paijostudio.hitungmasa.adapter.CarAdapter$MyViewHolder.onClick(CarAdapter.java:73)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

有什么解决方案可以显示DialogFragment吗?谢谢

最佳答案

由于您尝试在应用程序的两个不同区域中创建对话框,我建议将其移至它自己的类中

public class DialogHelper {
public static void infoMethod(FragmentManager fragmentManager) {
Bundle args = new Bundle();
args.putString("title", "Dialog with Action Bar");
ActionBarDialog actionbarDialog = new ActionBarDialog();
actionbarDialog.setArguments(args);
actionbarDialog.show(fragmentManager, "action_bar_frag");
}
}

然后在 SportFragment 中将您的调用替换为 DialogHelper.infoMethod(getActivity().getSupportFragmentManager());

对于您的 CarAdapter,您需要使用 FragmentManager

recyclerView.setAdapter(new CarAdapter(getActivity().getSupportFragmentManager()))

...

public class CarAdapter extends RecyclerView.Adapter<MyViewHolder> {
private FragmentManager fragmentManager;

public CarAdapter(FragmentManager fragmentManager) {
this.fragmentManager = fragmentManager;
}

public class MyViewHolder {
...

@Override
public void onClick(View v) {
if (v.getId() == image.getId()){
v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.imagelist));
DialogHelper.infoMethod(fragmentManager);

Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
}
}

关于java - 如何在 RecyclerView 适配器中显示 ImageView onClick 中的 Dialogfragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39763231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com