在Cardview中点击按钮打开DialogBox

时间:2018-01-31 10:37:38

标签: java android button onclicklistener alertdialog

我是android开发的初学者,我面临的问题是,一旦我点击编辑按钮,它在CardView上是不显示警报对话框,我将更新与该cardview相关的详细信息。

我尝试通过放置Toast来检查它是否正常工作,因此它可以工作,我甚至在主要活动中创建了一个单独的对话框,只是为了查看它是否正常工作,但是当我调用它时同样在使用该类对象的适配器中它会产生错误。

我正在从数据库中检索数据转换为Json,并且此数据在使用StringRequest和网络排球

的recyclelerview中显示在Cardview中

预期产量: 在Recycler视图中单击Cardview上的EditButton,它应该显示一个警告或对话框,它将包含我点击并在对话框中输入然后提交的卡的人的姓名。

有人节省了我的时间因为我花了很多时间尝试。谢谢你提前

适配器代码:

@Override
public void onBindViewHolder(MyClientTrackAdapter.ViewHolder holder, int position) {
    // Find out the data, based on this view holder's position
    final ListClientTrackIssue listClient = listClients.get(position);
    holder.textViewCustomer.setText(listClient.getCustomername());
    holder.textViewcurdate.setText(listClient.getCurdate());
    holder.textViewcurtime.setText(listClient.getCurtime());
    holder.textViewtargetdate.setText(listClient.getTargetdate());
    holder.textViewtargettime.setText(listClient.getTargettime());
    holder.textViewquery.setText(listClient.getQuery());
    holder.textViewaddress.setText(listClient.getAddress());
    holder.textViewassigned.setText(listClient.getAssignedperson());
    holder.textViewcomment.setText(listClient.getComment());
    holder.textViewstatus.setText(listClient.getStatus());

    holder.editbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(),"edit"+listClient.getCustomername(),Toast.LENGTH_SHORT).show();
            new AlertDialog.Builder(context)
                    .setMessage("client"+listClient.getCustomername())
                    .setTitle("Client Status update")
                    .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 listClients.size();
}

class ViewHolder extends RecyclerView.ViewHolder {
    Button editbutton;
    TextView textViewCustomer,textViewcurdate,textViewcurtime,textViewtargetdate,textViewtargettime,
                textViewquery,textViewaddress,textViewassigned,textViewcomment,textViewstatus;
    public ListCustomer listCustomer;

    public ViewHolder(View itemView) {
        super(itemView);

        textViewCustomer=itemView.findViewById(R.id.textViewCustomer);
        textViewcurdate=itemView.findViewById(R.id.textViewcurdate);
        textViewcurtime=itemView.findViewById(R.id.textViewcurtime);
        textViewtargetdate=itemView.findViewById(R.id.textViewtargetdate);
        textViewtargettime=itemView.findViewById(R.id.textViewtargettime);
        textViewquery=itemView.findViewById(R.id.textViewquery);
        textViewaddress=itemView.findViewById(R.id.textViewaddress);
        textViewassigned=itemView.findViewById(R.id.textViewassigned);
        textViewcomment=itemView.findViewById(R.id.textViewcomment);
        textViewstatus=itemView.findViewById(R.id.textViewstatus);

        this.editbutton=itemView.findViewById(R.id.editbtn);

主类代码:

public void showChangeLangDialog(){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.edit_dialog, null);
    dialogBuilder.setView(dialogView);

    //final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);

    dialogBuilder.setTitle("Update Client Status");
    dialogBuilder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //do something with edt.getText().toString();
        }
    });
    dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //pass
        }
    });
    AlertDialog b = dialogBuilder.create();
    b.show();
}

3 个答案:

答案 0 :(得分:1)

just create interface in adapter class and  
Interface 
public interface Clicked{
        void Buttonclick(View v,int position);
    }
    Clicked clicked;

button click
yourbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = (int) v.getTag();
                if (clicked != null) {
                    clicked.Buttonclick(v, pos);
                }
            }
        });
Adapter adapter=new Adapter(yourlist, new Adapter.Clicked() {
            @Override
            public void click(View view, int position) {
                if(view.getId()==your view id) { 
showdialog();
                }

            }
        });



this way you can get click of button in cardview in your activity/fragment and you can show dialog 

答案 1 :(得分:0)

holder.editbutton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(),"edit"+listClient.getCustomername(),Toast.LENGTH_SHORT).show();
        new AlertDialog.Builder(v.getRootView().getContext())
                .setMessage("client"+listClient.getCustomername())
                .setTitle("Client Status update")
                .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();

    }

});

答案 2 :(得分:0)

我是andriod开发的新手,但我在适配器中创建了自定义对话框:

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

            final Dialog dialog = new Dialog(mCtx);
            dialog.setContentView(R.layout.show_standerd_product);
            dialog.setTitle("Position" + position);
            dialog.setCancelable(true);

            ImageView d_image = dialog.findViewById(R.id.dialog_design_image);
            TextView d_type = dialog.findViewById(R.id.dialog_design_type);
            TextView d_name = dialog.findViewById(R.id.dialog_design_name);
            TextView d_cost = dialog.findViewById(R.id.dialog_design_cost);


            d_type.setText(standerdProduct.getType());
            d_name.setText(standerdProduct.getName());
            d_cost.setText("Cost :" + String.valueOf(standerdProduct.getAmount()));

            Glide.with(mCtx)
                    .load("http://yourSystemIpAddressforXAMP/darzee/" + standerdProduct.getImage())
                    .into(d_image);


            dialog.show();

        }
    });