本文介绍了自定义警告对话框,RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 RecyclerView 来列举一些文字,现在我想,这样当文本用户点击自定义警告对话框弹出来做到这一点。

到目前为止我试过,但得到一个NullPointerException异常;这可能是错在这里?

 公共类CBAdapter扩展RecyclerView.Adapter< CBAdapter.ViewHolder> {清单< AdapterData> mItems;公共CBAdapter(){
    超();
    mItems =新的ArrayList<>();
    AdapterData数据=新AdapterData();
    data.setTextOne(许多弓);
    mItems.add(数据);    数据=新AdapterData();
    data.setTextOne(对不起);
    mItems.add(数据);    数据=新AdapterData();
    data.setTextOne(下降到膝盖和放大器;求);
    mItems.add(数据);    数据=新AdapterData();
    data.setTextOne(后空翻);
    mItems.add(数据);}@覆盖
公共ViewHolder onCreateViewHolder(ViewGroup中ViewGroup中,int i)以{
    视图V = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.test3,ViewGroup中,FALSE);
    返回新ViewHolder(V);
}@覆盖
公共无效onBindViewHolder(ViewHolder viewHolder,int i)以{
    AdapterData数据= mItems.get(I)
    viewHolder.textOne.setText(data.getTextOne());}
@覆盖
公众诠释getItemCount(){    返回mItems.size();
}类ViewHolder扩展RecyclerView.ViewHolder {    公众的TextView textOne;
    私人上下文的背景下;    公共ViewHolder(查看ItemView控件){
        超(ItemView控件);
        textOne =(TextView中)itemView.findViewById(R.id.textView1);        itemView.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){                最后对话的对话=新的对话框(背景);
                dialog.setContentView(R.layout.custom_dialog);
                dialog.setTitle(标题);                TextView的文本=(TextView的)dialog.findViewById(R.id.text);
                text.setText(世界你好);                ImageView的图像=(ImageView的)dialog.findViewById(R.id.image);
                image.setImageResource(R.drawable.ic_launcher);                按钮dialogBu​​tton =(按钮)dialog.findViewById(R.id.dialogBu​​ttonOK);                dialogBu​​tton.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        dialog.dismiss();
                    }
                });                dialog.show();
            }
        });
    }
}
}


解决方案

没关系我忘了初始化我的上下文

=背景itemView.getContext();

I'm using RecyclerView to list some text and now I want to make it so that when the user clicks on text a custom Alert Dialog box pops up.

I have tried this so far but get a NullPointerException; what could be wrong here?

public class CBAdapter extends RecyclerView.Adapter<CBAdapter.ViewHolder> {

List<AdapterData> mItems;

public CBAdapter() {
    super();
    mItems = new ArrayList<>();
    AdapterData data = new AdapterData();
    data.setTextOne("Many Bows");
    mItems.add(data);

    data = new AdapterData();
    data.setTextOne("Pardon");
    mItems.add(data);

    data = new AdapterData();
    data.setTextOne("Fall To Knees & Beg");
    mItems.add(data);

    data = new AdapterData();
    data.setTextOne("Backflips");
    mItems.add(data);



}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.test3, viewGroup, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    AdapterData data = mItems.get(i);
    viewHolder.textOne.setText(data.getTextOne());

}


@Override
public int getItemCount() {

    return mItems.size();
}

class ViewHolder extends RecyclerView.ViewHolder{

    public TextView textOne;
    private Context context;





    public ViewHolder(View itemView) {
        super(itemView);
        textOne = (TextView)itemView.findViewById(R.id.textView1);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custom_dialog);
                dialog.setTitle("Title");

                TextView text = (TextView) dialog.findViewById(R.id.text);
                text.setText("hello world");

                ImageView image = (ImageView) dialog.findViewById(R.id.image);
                image.setImageResource(R.drawable.ic_launcher);

                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);

                dialogButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
        });


    }
}
}
解决方案

Nevermind I forgot the initialization my context

context = itemView.getContext();

这篇关于自定义警告对话框,RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:24