Start thread in OnClick in an adapter Android -
in application have adapter button. in onclicklistener of button must start thread modify ui, receive error. code is:
public view getview(final int position, view convertview, viewgroup parent) { final viewholder holder; if (convertview == null) { holder = new viewholder(); convertview = inflater.inflate(r.layout.detailnews_layout, parent, false); holder.commentbutton = (imageview) convertview .findviewbyid(r.id.imagebuttoncomment); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } final view commentview=convertview; holder.commentbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (menuposition == false) { holder.commentslayout.animate().translationy(-950) .setduration(2000); runnable commentsrun = new commentthread(position, commentview, holder); new thread(commentsrun).start(); menuposition = true; } else { holder.commentslayout.animate().translationy(-0) .setduration(500); menuposition = false; } } }); public class commentthread implements runnable { private int position; private view convertview; private viewholder holder; public commentthread(int position, view convertview, viewholder holder) { this.position = position; this.convertview = convertview; this.holder = holder; } public void run() { ... } }
but have error:
07-17 14:04:27.975: e/androidruntime(950): fatal exception: thread-97 07-17 14:04:27.975: e/androidruntime(950): android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views. 07-17 14:04:27.975: e/androidruntime(950): @ android.view.viewrootimpl.checkthread(viewrootimpl.java:4746) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.viewrootimpl.requestlayout(viewrootimpl.java:823) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.view.requestlayout(view.java:15473) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.view.requestlayout(view.java:15473) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.view.requestlayout(view.java:15473) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.view.requestlayout(view.java:15473) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.view.requestlayout(view.java:15473) 07-17 14:04:27.975: e/androidruntime(950): @ android.widget.relativelayout.requestlayout(relativelayout.java:318) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.view.requestlayout(view.java:15473) 07-17 14:04:27.975: e/androidruntime(950): @ android.widget.relativelayout.requestlayout(relativelayout.java:318) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.view.requestlayout(view.java:15473) 07-17 14:04:27.975: e/androidruntime(950): @ android.widget.relativelayout.requestlayout(relativelayout.java:318) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.view.requestlayout(view.java:15473) 07-17 14:04:27.975: e/androidruntime(950): @ android.widget.scrollview.requestlayout(scrollview.java:1440) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.view.requestlayout(view.java:15473) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.viewgroup.addview(viewgroup.java:3208) 07-17 14:04:27.975: e/androidruntime(950): @ android.view.viewgroup.addview(viewgroup.java:3155) 07-17 14:04:27.975: e/androidruntime(950): @ com.app.flipadapter.additem(flipadapter.java:447) 07-17 14:04:27.975: e/androidruntime(950): @ com.app.flipadapter.access$2(flipadapter.java:422) 07-17 14:04:27.975: e/androidruntime(950): @ com.app.flipadapter$commentthread.run(flipadapter.java:551) 07-17 14:04:27.975: e/androidruntime(950): @ java.lang.thread.run(thread.java:856)
the code in run method of thread it's ok , work out of onclick method. how con change code? thanks
you can use runonuithread
runonuithread(new runnable() { public void run() { //some jobe } });
if want update ui after complex job use asynctask , update ui onpostexecute
Comments
Post a Comment