简体   繁体   中英

Open DialogBox on click on button in Cardview

I am beginner in android development,The problem which im facing is that once i Click to Edit Button which is on CardView is Not Displaying Alert Dialog Box where i will update the details related to that cardview.

I tried making that Edit Button functionable by placing Toast to check if it is working and hence it worked,i even created a separate Dialog Box in main activity and just to see if it is working and it works but when i call the same in adapter using object of that class it gives error.

I am Retrieving Data from Database converting into Json and this data is Displayed in Cardview in recyclerview using StringRequest and volley for network

Expected output: Onclick of EditButton on Cardview in Recycler view it should display me an Alert or dialog box which will Consist Name of the person that is the Card which i clicked and input in dialog and then submit.

Someone save my time coz i have invested lots of time trying..Thank u in advance

Adapter code:

@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);

Main class code:

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();
}
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 
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();

    }

});

i am new in andriod developing but i created custom dialog in adapter this cod here:

  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();

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM