android - Calling a method from my MainActivity -
i've desperatly tried on internet able call thisisamethodjusttotryoutmybutton(view view)
, when user click delete button
mainactivity, nothing works. button gives no reaction.
java.lang.nullpointerexception: attempt invoke virtual method 'android.content.res.resources android.content.context.getresources()' on null object reference
adapter class
public class adapter extends baseadapter{ private arraylist<item> items= new arraylist<>(); public void additemtolistview(item item){ this.items.add(item); } public void deleteitem(int position){ items.remove(position); } @override public item getitem(int position) { return items.get(position); } @override public long getidofitem(int position) { return position; } @override public int getcount() { return items.size(); } class viewholder { public textview title, content; public viewholder(view view){ title= (textview) view.findviewbyid(r.id.title); content= (textview) view.findviewbyid(r.id.content); } } @override public view getview(final int position, view convertview, viewgroup parent) { viewholder viewholder; if(null == convertview){ layoutinflater inflater = layoutinflater.from(parent.getcontext()); convertview = inflater.inflate(r.layout.custom_row, parent, false); viewholder = new viewholder(convertview); viewholder.header.settag(position); convertview.settag(viewholder); } else{ viewholder = (viewholder) convertview.gettag(); } item item= getitem(position); viewholder.header.settext(item.title); viewholder.bodytext.settext(item.content); return convertview; } }
mydialogfordeletingaitem class
public class mydialogfordeletingaitem extends dialogfragment implements view.onclicklistener { button delete; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.deletedialog, null); delete = (button) view.findviewbyid(r.id.delete); delete.setonclicklistener(this); return view; } @override public void onclick(view view) { if (view.getid() == r.id.delete) { mainactivity main = new mainactivity(); main.thisisamethodjusttotryoutmybutton(view, //this problem is... //i don't know pass in here); }
mainactivity class
public class mainactivity extends appcompatactivity implements adapterview.onitemlongclicklistener, adapterview.onitemclicklistener { adapter adapter; listview listview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview = (listview) findviewbyid(r.id.listview); adapter= new adapter(); listview.setadapter(adapter); listview.setonitemlongclicklistener(this); listview.setonitemclicklistener(this); public void longitemclick(view view) { dialogfragment dialog = new mydialogfordeletingaitem(); dialog.show(this.getsupportfragmentmanager(), "mydialogfordeletingaitem"); thisisamethodjusttotryoutmybutton(view); } public void deleterow(int position){ adapter.delete(position); adapter.notifydatasetchanged(); } public void thisisamethodjusttotryoutmybutton(view view, int position){ deleterow(position); toast.maketext(mainactivity.this,"" + position, toast.length_short).show(); } <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="delete" android:id="@+id/delete" android:clickable="true" android:layout_aligntop="@+id/edit" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_marginright="102dp" android:layout_marginend="102dp" /> @ se.example.android.myapplication.mainactivity.thisisamethodjusttotryoutmybutton (mainactivity.java:100) @ se.example.android.myapplication.deleteandedit.onclick(deleteandedit.java:45)
on onclick(view view)
method creates new instance of mainactivity
while need use loaded activity instance.
call
@override public void onclick(view view) { if (view.getid() == r.id.delete) { ((mainactivity) getactivity()).thisisamethodjusttotryoutmybutton(view); }
happy_coding;
Comments
Post a Comment