onBindViewFolder中的AlertDialog用于从Card视图弹出

时间:2016-06-22 15:33:46

标签: android android-recyclerview alertdialog android-alertdialog android-cardview

我不是很高级的开发人员。目前我正在开发一个应用程序,其中必须从卡中的可点击图像调用alertdialog。我是这样开发的:

public class patient_detailsAdapter extends RecyclerView.Adapter<patient_detailsAdapter.patientDetailsViewHolder>{

    ArrayList<patient_details> patientDetailList = new ArrayList<patient_details>();
    Context mContext;

    public patient_detailsAdapter(ArrayList<patient_details> patientDetailList, Context mContext){

        this.patientDetailList = patientDetailList;
        this.mContext = mContext;

    }

    @Override
    public patientDetailsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.patient_list_layout,parent,false);
        patientDetailsViewHolder patientDetailsViewHolder = new patientDetailsViewHolder(view);

        return patientDetailsViewHolder;
    }

    @Override
    public void onBindViewHolder(final patientDetailsViewHolder holder, int position) {

        patient_details patient = patientDetailList.get(position);
        holder.person_img.setImageResource(patient.getPatientImage());
        holder.person_name.setText(patient.getName());
        holder.person_email.setText(patient.getEmail());
        holder.callButtonImage.setImageResource(patient.getCallButtonImage());

        holder.callButtonImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(mContext)
                        .setTitle("ABC")
                        .setMessage("bhnshsks")
                        .setIcon(R.drawable.call_now)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.i("Result","Success");
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.i("Result","Success");
                            }
                        })
                        .show();

            }
        });
    }

    @Override
    public int getItemCount() {
        return patientDetailList.size();
    }

    public static class patientDetailsViewHolder extends RecyclerView.ViewHolder{

        ImageView person_img,callButtonImage;
        TextView person_email,person_name;

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

            person_img = (ImageView) view.findViewById(R.id.profile_image);
            person_email = (TextView) view.findViewById(R.id.profile_email);
            person_name = (TextView) view.findViewById(R.id.profile_name);
            callButtonImage = (ImageView) view.findViewById(R.id.callButton);


        }

    }
}

但它显示错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference

对于使用v7小部件的警告对话框,我给它主题:

holder.callButtonImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.MyAlertDialogStyle);
            builder.setTitle("AppCompatDialog");
            builder.setMessage("Lorem ipsum dolor...");
            builder.setPositiveButton("OK", null);
            builder.setNegativeButton("Cancel", null);
            builder.show();

        }
    });

然后我收到了错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

但主要的是,当我删除了警告对话框,并使用Log.i测试了onclickListener时,我获得了成功。

请帮助,我到处搜索但没有发现任何有用的东西。 提前谢谢。

编辑

我的活动是:

public class PatientListActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener{

private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<patient_details> list ;
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_patient_list);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    list = new ArrayList<patient_details>();

    preparValue();

    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    adapter = new patient_detailsAdapter(list,context);
    recyclerView.setAdapter(adapter);
   }private void preparValue(){

    int[] images = new int[]{ R.drawable.abc, R.drawable.efg, R.drawable.efr};
    int[] buttonImage = new int[]{R.drawable.call_now};
    patient_details a = new patient_details(images[0],"abc@xyz.com","ABC_Name",buttonImage[0]);
    list.add(a);

    patient_details b = new patient_details(images[1],"efg@xyz.com","EFG_Name",buttonImage[0]);
    list.add(b);

    patient_details c = new patient_details(images[2],"efr@xyz.com","Leonardo Di Caprio",buttonImage[0]);
    list.add(c);

}

这是我实例化课程的活动。

1 个答案:

答案 0 :(得分:2)

您在适配器构造函数中传递的上下文为null。 您的活动扩展了上下文,因此您应该使用PatientListActivity

adapter = new patient_detailsAdapter(list,this);

而不是:

adapter = new patient_detailsAdapter(list,context);