android - How to use a progress bar when my app is reading data from a file ? Right before the start of the application -
i trying write application, many rows of data being extracted text file. done when start application. want show progress bar user, when clicks on application in applications screen. right there black screen brief period , shows application data , displays data. want add progress bar starting 0 100 or telling user application loading.
can me in adding progress bar code new this. appreciate given me. in advance.
public class movieratingsactivity extends listactivity { private arraylist<movie> movies = new arraylist<movie>(); private layoutinflater minflater; private lrucache<string, bitmap> cache; /** called when activity first created. */ public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); initializeui(); } private void initializeui() { minflater = (layoutinflater) this.getsystemservice(context.layout_inflater_service); inputstream inputstream = getresources().openrawresource( r.raw.ratings); movies = movie.loadfromfile(inputstream); setlistadapter(new rowiconadapter(this, r.layout.listrow, r.id.row_label, movies)); } static class viewholder { textview votestext; textview movietext; imageview icon; } /** custom row adatper -- displays icon next movie name */ class rowiconadapter extends arrayadapter<movie> { private arraylist<movie> movies; public rowiconadapter(context c, int rowresourceid, int textviewresourceid, arraylist<movie> items) { super(c, rowresourceid, textviewresourceid, items); movies = items; } public view getview(int pos, view convertview, viewgroup parent) { viewholder holder; movie currmovie = movies.get(pos); if (convertview == null) { convertview = minflater.inflate(r.layout.listrow, parent, false); holder = new viewholder(); holder.icon = (imageview) convertview.findviewbyid(r.id.row_icon); holder.movietext = (textview) convertview.findviewbyid(r.id.row_label); holder.votestext = (textview) convertview.findviewbyid(r.id.row_subtext); holder.movietext.settext(currmovie.getname()); string votesstr = currmovie.getvotes()+" votes"; holder.votestext.settext(votesstr); bitmap movieicon = getmovieicon(currmovie.getname(), currmovie.getrating()); holder.icon.setimagebitmap(movieicon); log.w("mvmvmvmvmvmv", "creating row view @ position "+pos+" movie "+currmovie.getname()); } return convertview; } } /** creates unique movie icon based on name , rating */ private bitmap getmovieicon(string moviename, string movierating) { int bgcolor = getcolor(moviename); bitmap b = bitmap.createbitmap(48, 48, bitmap.config.argb_8888); b.erasecolor(bgcolor); // fill bitmap color canvas c = new canvas(b); paint p = new paint(); p.setantialias(true); p.setcolor(gettextcolor(bgcolor)); p.settextsize(24.0f); c.drawtext(movierating, 8, 32, p); return b; } /** construct color movie name */ private int getcolor(string name) { string hex = tohexstring(name); string red = "#"+hex.substring(0,2); string green = "#"+hex.substring(2,4); string blue = "#"+hex.substring(4,6); string alpha = "#"+hex.substring(6,8); int color = color.argb(integer.decode(alpha), integer.decode(red), integer.decode(green), integer.decode(blue)); return color; } /** given movie name -- generate hex value hashcode */ private string tohexstring(string name) { int hc = name.hashcode(); string hex = integer.tohexstring(hc); if (hex.length() < 8) { hex = hex+hex+hex; hex = hex.substring(0,8); // use default color value } return hex; } /** crude optimization obtain contrasting color -- not work yet */ private int gettextcolor(int bg) { int r = color.red(bg); int g = color.green(bg); int b = color.blue(bg); string hex = integer.tohexstring(r)+integer.tohexstring(g); hex += integer.tohexstring(b); int cdec = integer.decode("#"+hex); if (cdec > 0xffffff/2) // go dark lighter shades return color.rgb(0, 0, 0); else { r = (r+128)%256; g = (g+128)%256; b = (b+128)%256; return color.rgb(r,g,b); } } }
http://android-er.blogspot.be/2010/11/progressbar-running-in-asynctask.html
onpostexecute ==> reading done, next
onpreexecute ==> going start reading first
doinbackground ==> reading data
onprogressupdate ==> update progressbar
with u should able create progressbar
Comments
Post a Comment