i trying load uri
sound files sd card custom gridview
each item contains imageview
(for uri
) , textview
sound file title, keeps loading duplicates. have 6 sound files on card, loads 12, oddly 3 after drawables load. drawables (placeholder images) load fine, load right number.
i put lot of file i/o asynctask
, doubt cause problems this.
when log everything, see things getting unexpectedly loaded few times (console below), including odd null values. have other working code exact same process, except loading images instead of sound files, , works perfectly, puzzling. realize loading uri
s gridview
not ideal, there nothing see in imageview
, when click on each item, play file, part correct. rework image problem later.
any idea why things loading strangely in duplicates? thanks.
update
it turns out since had arraylist<audiogriditem>
in adapter constructor, change list automatically updating, didn't need add again adapter manually. deleted adding adapter in onpostexecute()
, worked!
@override protected void onpostexecute(audiogriditem result) { progressdialog.dismiss(); // add default icons remaining, gridview, if less 15 files on sd card (int = 0; < (15 - numbersdcardfiles.length); i++) { audioadapter.add(drawable); } audioadapter.notifydatasetchanged(); }
audiotab.java
package org.azurespot.cutecollection.audiotab; import android.app.progressdialog; import android.media.mediaplayer; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.os.environment; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.gridview; import org.azurespot.r; import java.io.file; import java.io.ioexception; import java.util.arraylist; /** * created mizu on 2/8/15. */ public class audiotab extends fragment { private gridview gridview; private gridviewaudioadapter audioadapter; private progressdialog progressdialog; private string[] numbersdcardfiles = null; file[] files; arraylist<audiogriditem> audiofiles = new arraylist<>(); mediaplayer mp; audiogriditem audiogriditem; audiogriditem drawable; public audiotab(){ super(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v = inflater.inflate(r.layout.audio_tab, container, false); mp = new mediaplayer(); // instantiate progress dialog progressdialog = new progressdialog(getactivity()); // fragments, make sure include rootview when finding id gridview = (gridview) v.findviewbyid(r.id.audio_grid); // create custom adapter object audioadapter = new gridviewaudioadapter(getactivity(), audiofiles); // set adapter gridview gridview.setadapter(audioadapter); log.d("tag", "items in audioadapter1: " + audioadapter.getcount()); if(audioadapter.getcount() == 0) { // load contents of sd card through asynctask new audiodownloadertask().execute(); } setupgridviewlistener(); return v; } private class audiodownloadertask extends asynctask<object, void, audiogriditem> { @override protected void onpreexecute() { super.onpreexecute(); progressdialog.setmessage("loading cute collection ..."); //set progress bar cancelable on button progressdialog.setcancelable(true); progressdialog.show(); } @override protected audiogriditem doinbackground(object... params) { retrieveaudio(); return null; } @override protected void onpostexecute(audiogriditem result) { progressdialog.dismiss(); log.d("tag", "items in audiofiles: " + audiofiles.size()); // add whole arraylist adapter audioadapter.addall(audiofiles); log.d("tag", "number of item in audioadapter2: " + audioadapter.getcount()); // add default icons remaining, gridview, if less 15 files on sd card (int = 0; < (15 - numbersdcardfiles.length); i++) { audioadapter.add(drawable); } audioadapter.notifydatasetchanged(); log.d("tag", "number of item in audioadapter3: " + audioadapter.getcount()); log.d("tag", "number of items in sd card files: " +numbersdcardfiles.length); } } public void retrieveaudio() { try { // gets directory cute videos sd card file cutevideosdir = new file(environment.getexternalstoragepublicdirectory (environment.directory_podcasts), "cute sounds"); // puts list files array files = cutevideosdir.listfiles(); // number of files in cute sounds directory numbersdcardfiles = new string[files.length]; (file singlefile : files) { // both audio file , audio title uri audiouri = uri.fromfile(singlefile); string audiotitle = singlefile.getname(); audiogriditem = new audiogriditem(audiouri, audiotitle); // add uri , title arraylist audiofiles.add(audiogriditem); } // changes drawable audiogriditem object (uri, no string) drawable = new audiogriditem(uri.parse("android.resource://org.azurespot/" + r.drawable.ic_sounds_placeholder), null); } catch (exception e) { e.printstacktrace(); } } private void setupgridviewlistener() { gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view item, int pos, long id) { if(!(audioadapter.getitem(pos).equals(drawable))) { uri soundfile = (audioadapter.getitem(pos)).getaudio(); try { mp.setdatasource(getactivity(), soundfile); mp.prepare(); mp.start(); } catch (ioexception e) { e.printstacktrace(); } } } }); } }
gridviewaudioadapter.java
package org.azurespot.cutecollection.audiotab; import android.content.context; import android.net.uri; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.linearlayout; import android.widget.textview; import org.apache.commons.io.filenameutils; import org.azurespot.r; import java.util.arraylist; /** * created mizu on 2/8/15. */ public class gridviewaudioadapter extends arrayadapter<audiogriditem> { private textview audiotitleview; int position; viewholder holder = null; public gridviewaudioadapter(context context, arraylist<audiogriditem> audio) { super(context, 0, audio); } @override public view getview(int position, view itemview, viewgroup parent) { this.position = position; if (itemview == null) { itemview = layoutinflater.from(getcontext()) .inflate(r.layout.audio_tab_item, parent, false); holder = new viewholder(); holder.audioview = (imageview) itemview.findviewbyid(r.id.audio_icon); audiotitleview = (textview) itemview.findviewbyid(r.id.audio_title); // stores holder view itemview.settag(holder); } else { holder = (viewholder)itemview.gettag(); } // position of item clicked in gridview final audiogriditem audiogriditem = getitem(position); if (audiogriditem != null) { uri audiouri = audiogriditem.getaudio(); string audiotitle = audiogriditem.getaudiotitle(); string rootname = filenameutils.removeextension(audiotitle); log.d("tag", "value of audiotitle: " + audiotitle); log.d("tag", "value of rootname: " + rootname); // set photos imageview slots holder.audioview.setimageuri(audiouri); audiotitleview.settext(rootname); // positioning image in gridview slot holder.audioview.setscaletype(imageview.scaletype.center_crop); holder.audioview.setlayoutparams(new linearlayout.layoutparams (250, 250)); } return itemview; } public class viewholder{ imageview audioview; } }
here model: audiogriditem.java
package org.azurespot.cutecollection.audiotab; import android.net.uri; /** * created mizu on 4/26/15. */ public class audiogriditem { private uri audio; private string audiotitle; public audiogriditem(uri audio, string autotitle) { super(); this.audio = audio; this.audiotitle = autotitle; } public uri getaudio() { return audio; } public void setaudio(uri audio){ this.audio = audio; } public string getaudiotitle(){ return audiotitle; } public void setaudiotitle(string audiotitle){ this.audiotitle = audiotitle; } }
logs
04-27 16:46:56.406 24668-24668/org.azurespot d/viewrootimpl﹕ viewpostimeinputstage action_down 04-27 17:04:28.176 24668-24668/org.azurespot d/viewrootimpl﹕ viewpostimeinputstage action_down 04-27 17:04:28.326 24668-24668/org.azurespot d/tag﹕ items in audioadapter1: 0 04-27 17:04:28.376 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:28.376 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:28.386 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:28.386 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:28.446 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:28.446 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:28.446 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:28.446 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:28.466 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:28.466 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:28.466 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound66.3gpp 04-27 17:04:28.466 24668-24668/org.azurespot d/tag﹕ value of rootname: sound66 04-27 17:04:28.476 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound19.3gpp 04-27 17:04:28.476 24668-24668/org.azurespot d/tag﹕ value of rootname: sound19 04-27 17:04:28.476 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound47.3gpp 04-27 17:04:28.486 24668-24668/org.azurespot d/tag﹕ value of rootname: sound47 04-27 17:04:28.486 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound49.3gpp 04-27 17:04:28.486 24668-24668/org.azurespot d/tag﹕ value of rootname: sound49 04-27 17:04:28.486 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound51.3gpp 04-27 17:04:28.486 24668-24668/org.azurespot d/tag﹕ value of rootname: sound51 04-27 17:04:28.606 24668-24668/org.azurespot d/tag﹕ items in audiofiles: 6 04-27 17:04:28.606 24668-24668/org.azurespot d/tag﹕ number of item in audioadapter2: 12 04-27 17:04:28.606 24668-24668/org.azurespot d/tag﹕ number of item in audioadapter3: 21 04-27 17:04:28.606 24668-24668/org.azurespot d/tag﹕ number of items in sd card files: 6 04-27 17:04:28.616 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:28.616 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:28.616 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:28.616 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:28.616 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:28.616 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:28.616 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:28.616 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:28.626 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:28.626 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:28.626 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound66.3gpp 04-27 17:04:28.626 24668-24668/org.azurespot d/tag﹕ value of rootname: sound66 04-27 17:04:28.626 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound19.3gpp 04-27 17:04:28.626 24668-24668/org.azurespot d/tag﹕ value of rootname: sound19 04-27 17:04:28.626 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound47.3gpp 04-27 17:04:28.636 24668-24668/org.azurespot d/tag﹕ value of rootname: sound47 04-27 17:04:28.636 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound49.3gpp 04-27 17:04:28.636 24668-24668/org.azurespot d/tag﹕ value of rootname: sound49 04-27 17:04:28.636 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound51.3gpp 04-27 17:04:28.636 24668-24668/org.azurespot d/tag﹕ value of rootname: sound51 04-27 17:04:28.636 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:28.636 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:28.636 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound66.3gpp 04-27 17:04:28.636 24668-24668/org.azurespot d/tag﹕ value of rootname: sound66 04-27 17:04:28.646 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound19.3gpp 04-27 17:04:28.646 24668-24668/org.azurespot d/tag﹕ value of rootname: sound19 04-27 17:04:28.646 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound47.3gpp 04-27 17:04:28.646 24668-24668/org.azurespot d/tag﹕ value of rootname: sound47 04-27 17:04:28.646 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound49.3gpp 04-27 17:04:28.646 24668-24668/org.azurespot d/tag﹕ value of rootname: sound49 04-27 17:04:28.656 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound51.3gpp 04-27 17:04:28.656 24668-24668/org.azurespot d/tag﹕ value of rootname: sound51 04-27 17:04:28.656 24668-24668/org.azurespot d/tag﹕ value of audiotitle: null 04-27 17:04:28.656 24668-24668/org.azurespot d/tag﹕ value of rootname: null 04-27 17:04:28.666 24668-24668/org.azurespot d/tag﹕ value of audiotitle: null 04-27 17:04:28.666 24668-24668/org.azurespot d/tag﹕ value of rootname: null 04-27 17:04:28.666 24668-24668/org.azurespot d/tag﹕ value of audiotitle: null 04-27 17:04:28.666 24668-24668/org.azurespot d/tag﹕ value of rootname: null 04-27 17:04:29.876 24668-24668/org.azurespot d/viewrootimpl﹕ viewpostimeinputstage action_down 04-27 17:04:30.516 24668-24668/org.azurespot d/tag﹕ value of audiotitle: null 04-27 17:04:30.516 24668-24668/org.azurespot d/tag﹕ value of rootname: null 04-27 17:04:30.516 24668-24668/org.azurespot d/tag﹕ value of audiotitle: null 04-27 17:04:30.516 24668-24668/org.azurespot d/tag﹕ value of rootname: null 04-27 17:04:30.516 24668-24668/org.azurespot d/tag﹕ value of audiotitle: null 04-27 17:04:30.516 24668-24668/org.azurespot d/tag﹕ value of rootname: null 04-27 17:04:31.696 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound54.3gpp 04-27 17:04:31.696 24668-24668/org.azurespot d/tag﹕ value of rootname: sound54 04-27 17:04:31.696 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound66.3gpp 04-27 17:04:31.696 24668-24668/org.azurespot d/tag﹕ value of rootname: sound66 04-27 17:04:31.696 24668-24668/org.azurespot d/tag﹕ value of audiotitle: sound19.3gpp 04-27 17:04:31.696 24668-24668/org.azurespot d/tag﹕ value of rootname: sound19
check out
audioadapter = new gridviewaudioadapter(getactivity(), audiofiles);
the above line audiofiles
empty arraylist
and audioadapter
looking backup man.. in retrieveaudio()
method add items audiofiles
audiofiles.add(audiogriditem);
audiofiles
contains 1 item, -(suppose item add) in onpostexecute
call
log.d("tag", "items in audiofiles: " + audiofiles.size()); // add whole arraylist adapter audioadapter.addall(audiofiles); //pay attention here log.d("tag", "number of item in audioadapter2: " + audioadapter.getcount());
you have added audiofiles
audiofiles
giving total of 2 items in gridview
documentation addall(collection) adds specified collection @ end of array means not replace it, got forget onpostexecute
adding , use on onprogressupdate
add item 1 one , call notifydatasetchanged()
because can't call in doinbackground