android - showDialog in button Listview adapter -


i have listview this

enter image description here

i want when press delete. show dialog image

enter image description here

so when press yes. remove list.

here's code..

public class customadapter extends baseadapter{   arraylist<hashmap<string, string>> oslist; context context; private button btndelete; private button btnedit; alertdialog.builder alertdialogbuilder;  public customadapter(arraylist<hashmap<string, string>> oslist,context context) {       system.out.println("skdjfhksdfjskfjhsdkjfh");     this.context = context;     this.oslist = oslist;  }  @override public int getcount() {     // todo auto-generated method stub           return oslist.size(); }  @override public object getitem(int position) {     // todo auto-generated method stub     return oslist.get(position); }  @override public long getitemid(int position) {     // todo auto-generated method stub     return position; }  @override public view getview(final int position, view convertview, viewgroup parent) {     system.out.println("oslist oslist = "+oslist);     system.out.println("oslist 1 = "+oslist);     system.out.println("oslist size = "+oslist.size());     system.out.println("oslist oslist = "+oslist.getclass());     system.out.println("position = "+position);     system.out.println("convertview = "+convertview);     system.out.println("parent = "+parent);      system.out.println("position =  "+position);          layoutinflater lif = (layoutinflater) context.getsystemservice(context.layout_inflater_service);     convertview = lif.inflate(r.layout.listitem, null);      textview id = (textview) convertview.findviewbyid(r.id.varid);       textview nonota = (textview) convertview.findviewbyid(r.id.varnonota);     textview sendername = (textview) convertview.findviewbyid(r.id.varsendername);     textview totalamount = (textview) convertview.findviewbyid(r.id.vartotalamount);      id.settext(oslist.get(position).get("id"));     nonota.settext(oslist.get(position).get("nonota"));     sendername.settext(oslist.get(position).get("sendername"));     totalamount.settext(oslist.get(position).get("totalamount"));         button btnedit = (button) convertview.findviewbyid(r.id.btnedit);     button btndelete = (button) convertview.findviewbyid(r.id.btndelete);     btnedit.setonclicklistener(new onclicklistener() {         @override         public void onclick(view v) {              toast.maketext(context, "edit ditekan!", toast.length_long).show();             //i want show yes no dialog here.                    }     });      btndelete.setonclicklistener(new onclicklistener() {                         @override         public void onclick(view v) {             //i want show yes no dialog here         }     });         return convertview; } 

}

how can create dialog that..i tried code

final dialog dialog = new dialog(context);                 dialog.setcontentview(r.layout.custom);                 dialog.settitle("title...");                  // set custom dialog components - text, image , button                 textview text = (textview) dialog.findviewbyid(r.id.text);                 text.settext("android custom dialog example!");                 imageview image = (imageview) dialog.findviewbyid(r.id.image);                 image.setimageresource(r.drawable.ic_launcher); //line 115                  button dialogbutton = (button) dialog.findviewbyid(r.id.dialogbuttonok);                 // if button clicked, close custom dialog                 dialogbutton.setonclicklistener(new onclicklistener() {                     @override                     public void onclick(view v) {                         dialog.dismiss();                     }                 });                  dialog.show(); 

but itsnt success.

i got error

enter image description here

create interface:

public interface ondeletelistener {     public void ondelete(string message); } 

when initializing customadapter send ondeletelistener 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 listener whether activate 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 want show");         }     });   

and initialize adapter in activity/fragment , 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 dialog here     //msg - can send parameter or none of them through interface example send message show     showdialog(msg); } }); 

you can create seperate function code clearance , call whenever want use

(also notice that, create custom dialog have inflate {probably thats why getting error}):

private void showdialog(string message){ // set custom dialog components - text, image , 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: have hardcoded answer syntax error can occur


Comments