Skip to content Skip to sidebar Skip to footer

Showdialog In Button Listview Adapter

i have a listView like THIS i want when i press the delete. it show a dialog like this image so when i press YES. it will remove from list. here's my code.. public class customad

Solution 1:

Create an interface:

publicinterfaceOnDeleteListener {
    publicvoidonDelete(String message);
}

When you are initializing customadapter send OnDeleteListener as a parameter:

privateOnDeleteListener mListener;
publiccustomadapter(ArrayList<HashMap<String, String>> oslist,Context context, OnDeleteListener mListener) {  
    this.context = context;
    this.oslist = oslist;
    this.mListener = mListener;
}

then on delete button click check for listener to whether activate it or not:

 btnDelete.setOnClickListener(newOnClickListener() {                
        @OverridepublicvoidonClick(View v) {
            //I want show YES NO dialog hereif(mListener != null)
              mListener.onDelete("The message you want to show");
        }
    });  

and finally initialize adapter in your activity/fragment and on listener invoke show Dialog:

customadaper mAdapter = newcustomadapter(ArrayList<HashMap<String, String>> oslist,Context context, newOnDeleteListener(){
   @OverridepublicvoidonDelete(String msg){
    //Show your dialog here//msg - you can send any parameter or none of them through interface just as an example i send a message to showshowDialog(msg);
}
});

You can create a seperate function for code clearance and call it whenever you want to use

(also notice that, to create a custom dialog you have to inflate it {probably thats why you are getting an error}):

privatevoidshowDialog(String message){
// set the custom dialog components - text, image and button
inflater = mInflater.inflate(R.layout.your_custom_dialog, null, false);
TextViewtext= (TextView) inflater.findViewById(R.id.text);
text.setText(message);
ImageViewimage= (ImageView) inflater.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher); //line 115

AlertDialog.BuildermDialogBuilder=newAlertDialog.Builder(context);
             mDialogBuilder.setView(viewFilterDialog);
             mDialogBuilder.setCancelable(true);
mDialogBuilder.setTitle(mRes.getString(R.string.dialog_title_filter));
             ...

finalAlertDialogmAlertDialog= mDialogBuilder.create();
mAlertDialog.show();

note: I have hardcoded this answer so any syntax error can occur

Solution 2:

try this code.For a Dialog you have to use Activity's instance not application context.

finalDialogdialog=newDialog(youractivity.this);
dialog.setContentView(R.layout.custom);

Solution 3:

You can also do this in activity. For Adapter code, add some tags to your button to identify which button is pressed. You can also set position as a tag.

Button btnEdit = (Button) convertView.findViewById(R.id.btnEdit);
Button btnDelete = (Button) convertView.findViewById(R.id.btnDelete);

btnEdit.setTag(oslist.get(position).get("id"),R.id.varId);
btnDelete .setTag(oslist.get(position).get("id"),R.id.varId);

You can add this in xml for your custom dialog. android:onclick="deleteMethod"

Write your method in activity like this :

publicvoiddeleteMethod(View v)
{
// Get your id or position for which delete button was pressed
 String id=v.getTag().toString();

 new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you really want to delete ?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

    publicvoidonClick(DialogInterface dialog, int whichButton) {
        Toast.makeText(MainActivity.this, "Deleting...", Toast.LENGTH_SHORT).show();
// You know which item to delete from id / position. delete here.
    }})
 .setNegativeButton(android.R.string.no, null).show();



}

Solution 4:

I think there is a easy way to do this, in my case i have followed this code. write a method alertMessage() and call this inside your button listener code

btnDelete.setOnClickListener(newOnClickListener() {                
        @OverridepublicvoidonClick(View v) {
            //I want show YES NO dialog here
              alertMessage();
        }
    }); 



publicvoidalertMessage() { 
    DialogInterface.OnClickListenerdialogClickListener=newDialogInterface.OnClickListener() {
    publicvoidonClick(DialogInterface dialog, int which) { 
      switch (which) { 
         case DialogInterface.BUTTON_POSITIVE: // Yes button clicked 
            Toast.makeText(AlertDialogActivity.this, "Yes Clicked",
                             Toast.LENGTH_LONG).show(); 
             // set your own logic for removal item from listview      break; 

            case DialogInterface.BUTTON_NEGATIVE: // No button clicked // do nothing 
             Toast.makeText(AlertDialogActivity.this, "No Clicked", 
                              Toast.LENGTH_LONG).show();    
             break; 
          } 
        } 
    }; 

   AlertDialog.Builderbuilder=newAlertDialog.Builder(this);  
   builder.setMessage("Are you sure?") 
           .setPositiveButton("Yes", dialogClickListener) 
           .setNegativeButton("No", dialogClickListener).show(); 
}

dialog will dismiss itself after user click "YES" or "NO" button. There is nothing you need to do

Post a Comment for "Showdialog In Button Listview Adapter"