showDialog in button Listview adapter

£可爱£侵袭症+ 提交于 2019-11-28 12:50:36

Create an interface:

public interface OnDeleteListener {
    public void onDelete(String message);
}

When you are initializing customadapter send OnDeleteListener as a parameter:

private OnDeleteListener mListener;
public customadapter(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(new OnClickListener() {                
        @Override
        public void onClick(View v) {
            //I want show YES NO dialog here
            if(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 = new customadapter(ArrayList<HashMap<String, String>> oslist,Context context, new OnDeleteListener(){
   @Override
   public void onDelete(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 show
    showDialog(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}):

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

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

final AlertDialog mAlertDialog = mDialogBuilder.create();
mAlertDialog.show();

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

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

final Dialog dialog = new Dialog(youractivity.this);
dialog.setContentView(R.layout.custom);

first pass the activity like this.

list.setAdapter(new customadapter(oslist,getApplicationContext(),registerItem.this));

then get the parent activity like this..

private Activity parentActivity;
    private Button btnDelete;
    private Button btnEdit;

    AlertDialog.Builder alertDialogBuilder;

    public customadapter(ArrayList<HashMap<String, String>> oslist,Context context, Activity parentactivity) {  
        System.out.println("skdjfhksdfjskfjhsdkjfh");
        this.context = context;
        this.oslist = oslist;
        this.parentActivity = parentactivity;

    }

and set dialog builder like this..

final Dialog dialog = new Dialog(parentActivity);

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 :

public void deleteMethod(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() {

    public void onClick(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();



}

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(new OnClickListener() {                
        @Override
        public void onClick(View v) {
            //I want show YES NO dialog here
              alertMessage();
        }
    }); 



public void alertMessage() { 
    DialogInterface.OnClickListener dialogClickListener = new  DialogInterface.OnClickListener() {
    public void onClick(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.Builder builder = new AlertDialog.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!