android - AsyncTask textview redrawing -
i little bit beginner android, , cannot explain 1 things code.
i have textview , button on activity. on button click start asynctask child class giving activity instance constructor. long running asynctask updates textview in progress update, during textview not cleared , redrawn, instead numbers written on top of each other.
it not important why solved async now, part of test, interesting question me right why happens this. have forget important property setting textview or what?
<textview android:id="@+id/textview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="60dip" android:layout_marginbottom="20dip" android:text="large text" android:gravity="center_horizontal" android:textsize="50sp"/>
and code:
public class asynctask1 extends activity { public textview txt; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_async_task1); this.txt = (textview) findviewbyid(r.id.textview1); } public void onclick(view v) { myasync m = new myasync(this); m.execute(""); } }
and here asynctask derivation:
class myasync extends asynctask<string,void,string> { asynctask1 pld = null; public myasync(asynctask1 pld) { this.pld = pld; } int = 0; @override public string doinbackground(string... n) { for(i=0;i<20;i++) { try { thread.sleep(1000); } catch (exception e) {} publishprogress(); } return null; } @override public void onprogressupdate(void... values) { pld.txt.settext(string.valueof(i)); } }
invalidate function redraw view. pld.txt.invalidate();
Comments
Post a Comment