android - Image of item changes when scroll up and down listview -
i have used custom base adapter listview. each row contains progressbar, imagebutton , 2 textview. have made audio player each row play button (which changes image pause on clicking) , progressbar (which starts showing progress button clicks). problem arise when scroll list:
1) when scroll down , back, image of imagebutton returns play , progressbar clears progress although song still playing.
2) other items of listview starts showing pause images on imagebutton , progressbar shows progress of song being played(i think after every 6th row 7th 1 that). please me. here custom adapter:
public class audioadapter extends baseadapter { context context; uri[] uris; layoutinflater layout; imagebutton previousbutton = null; progressbar previousprogressbar = null; static private handler myhandler = new handler(); public audioadapter(context context, int resource, uri[] uris) { // todo auto-generated constructor stub this.context = context; this.uris = uris; } class audioviewholder { imagebutton play; textview date, duration; progressbar progressbar; public audioviewholder(view v) { // todo auto-generated constructor // stub play = (imagebutton) v.findviewbyid(r.id.imagebutton_rowaudioplay); date = (textview) v.findviewbyid(r.id.textview_rowaudiodate); duration = (textview) v .findviewbyid(r.id.textview_rowaudioduration); progressbar = (progressbar) v .findviewbyid(r.id.progressbar_rowaudioplay); } } @override public int getcount() { // todo auto-generated method stub return uris.length; } @override public view getview(int position, view convertview, viewgroup parent) { view row = convertview; final audioviewholder holder; if (row == null) { layout = (layoutinflater) context .getsystemservice(context.layout_inflater_service); row = layout.inflate(r.layout.row_hometab_audio, parent, false); row.setvisibility(view.invisible); holder = new audioviewholder(row); row.settag(holder); } else { holder = (audioviewholder) row.gettag(); } holder.duration.settext(audioplayer.getaudiominutes(context, uris[position]) + ":" + audioplayer.getaudioseconds(context, uris[position])); debugmessage.logmessage("inside audioadapter getview() . isrownull = " + (row == null)); holder.progressbar.settag(position); holder.play.settag(position); holder.play.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub int row_position = (integer) v.gettag(); if (holder.play == previousbutton && audioplayer.isplaying) audioplayer.pauseaudio(holder); else { if (holder.play == previousbutton && audioplayer.isplaying == false) { audioplayer.resumeaudio(holder); audioplayer.progressbarupdate(holder, myhandler); } else if (audioplayer.isplaying) { audioplayer.stopaudio(previousbutton, previousprogressbar); audioplayer.playaudio(context, uris[row_position], holder); audioplayer.progressbarupdate(holder, myhandler); holder.play .setimageresource(android.r.drawable.ic_media_pause); previousbutton = holder.play; previousprogressbar = holder.progressbar; audioplayer.isplaying = true; } else { audioplayer.playaudio(context, uris[row_position], holder); audioplayer.progressbarupdate(holder, myhandler); holder.play .setimageresource(android.r.drawable.ic_media_pause); previousbutton = holder.play; previousprogressbar = holder.progressbar; audioplayer.isplaying = true; } } audioplayer.onaudiofinished(holder); } }); row.setvisibility(view.visible); return row; } @override public object getitem(int position) { // todo auto-generated method stub return null; } @override public long getitemid(int position) { // todo auto-generated method stub return 0; }
here player:
public class audioplayer { static mediaplayer mediaplayer = null; static string time; public static boolean isplaying = false; static int currprogress; public static void progressbarupdate( final audioadapter.audioviewholder holder, final handler myhandler) { myhandler.postdelayed(new runnable() { @override public void run() { // todo auto-generated method stub currprogress = mediaplayer.getcurrentposition(); holder.progressbar.setprogress(currprogress); myhandler.postdelayed(this, 100); } }, 100); } public static void playaudio(context context, uri filepath, audioadapter.audioviewholder holder) { mediaplayer = mediaplayer.create(context, filepath); mediaplayer.setlooping(false); holder.progressbar.setvisibility(view.visible); holder.progressbar.setmax(mediaplayer.getduration()); holder.play.setimageresource(android.r.drawable.ic_media_pause); mediaplayer.start(); } public static void pauseaudio(audioadapter.audioviewholder holder) { mediaplayer.pause(); holder.progressbar.setvisibility(view.invisible); holder.play.setimageresource(android.r.drawable.ic_media_play); } public static void stopaudio(imagebutton play,progressbar progressbar) { mediaplayer.stop(); mediaplayer.release(); progressbar.setvisibility(view.invisible); play.setimageresource(android.r.drawable.ic_media_play); } public static void resumeaudio(audioadapter.audioviewholder holder) { // todo auto-generated method stub mediaplayer.start(); holder.progressbar.setvisibility(view.visible); holder.progressbar.setmax(mediaplayer.getduration()); holder.play.setimageresource(android.r.drawable.ic_media_pause); } public static void onaudiofinished(final audioadapter.audioviewholder holder) { mediaplayer.setoncompletionlistener(new oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { // todo auto-generated method stub debugmessage.logmessage("audio completed"); holder.progressbar.setvisibility(view.invisible); holder.play.setimageresource(android.r.drawable.ic_media_play); } }); } public static string getaudiominutes(context context, uri filepath) { int duration = mediaplayer.create(context, filepath).getduration(); int min = (int) timeunit.milliseconds.tominutes((long) duration); if (min < 10) return string.valueof("0" + min); else return string.valueof(min); } public static string getaudioseconds(context context, uri filepath) { int duration = mediaplayer.create(context, filepath).getduration(); long sec = (long) (timeunit.milliseconds.toseconds((long) duration) - timeunit.minutes .toseconds(timeunit.milliseconds.tominutes((long) duration))); if (sec < 10) return string.valueof("0" + sec); else return string.valueof(sec); }
anyone have idea?
the logic within click event, changes views assign images , visibility...that needs happen every getview()
call.
so lets playing song x
. if view scrolls off screen, , comes on screen...it'll fire off getview()
. getview()
call needs able restore view state of playing song.
so ideally, every getview()
need way say, "hey, have uri. please tell me if it's playing , how many seconds along it." update views accordingly.
Comments
Post a Comment