java - Change all ImageViews in ListView to a particular image -
i'm trying make audio file preview page consists of listview
of files in folder. each row
has play button
next changes stop button
when clicked. need way of setting other images
play button
if row clicked 1 audio file
should playing @ once. tried iterating through list , getting imageview
had no luck (shown below):
public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); incidentlogger logger = new incidentlogger(this.getactivity()); final list<audio> audiolist = logger.getallaudio(); audiolistviewadapter adapter = new audiolistviewadapter(audiolist, getactivity()); final listview listview = (listview) getactivity().findviewbyid(r.id.listview); listview.setadapter(adapter); onitemclicklistener onitemclicklistener = new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { for(int = 0; < audiolist.size(); i++) { view v = listview.getadapter().getview(i, view, parent); imageview imageview = (imageview) v.findviewbyid(r.id.control); imageview.setimageresource(r.drawable.ic_action_play); } imageview iv = (imageview) view.findviewbyid(r.id.control); if(iv.gettag() == "1") { iv.settag("0"); iv.setimageresource(r.drawable.ic_action_play); } else { iv.settag("1"); iv.setimageresource(r.drawable.ic_action_stop); } } }; listview.setonitemclicklistener(onitemclicklistener); }
this can done easily, need @ on position of list item clicked user. described complete demo code below ...
public class changeimageinlistdynamically extends activity { /** * adapter data. */ imageadapter adpar; arraylist<string> itemlist; listview mlistview; imageadapter madapter; /** * indicate item selected. * default selected item '0' e.g., 1st item. */ private int selectedposition=0; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.main); itemlist = new arraylist<string>(); (int = 0; < 11; i++) { itemlist.add("item - "+i); } mlistview = (listview)findviewbyid(r.id.data_list); //initialize adapter madapter = new imageadapter(nchangeimageinlistdynamically.this, itemlist); //set adapter list. mlistview.setadapter(madapter); //set list click listener. mlistview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> av, view view, int position, long id) { //your image need change dynamically. imageview imageview = (imageview) view.findviewbyid(r.id.select); //position of clicked item. selectedposition=position; //change image of respective image. imageview.setimageresource(r.drawable.selected); //notify adapter update list. adpar.notifydatasetchanged(); } }); } static class viewholder { textview title; imageview img; } /** * imageadapter. */ public class imageadapter extends baseadapter { arraylist<string> datalist; context context; public imageadapter(context context, arraylist<string> arraylist) { this.context = context; this.datalist = arraylist; } @override public int getcount() { // todo auto-generated method stub return datalist.size();; } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(int position, view convertview, viewgroup parent) { view row = convertview; final viewholder holder; log.d("posiiton imageadapater : ",""+position); if (row == null) { layoutinflater inflater = changeimageinlistdynamically.this.getlayoutinflater(); row = inflater.inflate(r.layout.listrow, null); holder = new viewholder(); holder.title = (textview) row.findviewbyid(r.id.text); holder.img = (imageview) row.findviewbyid(r.id.select); row.settag(holder); } else { holder = (viewholder) row.gettag(); } //set titel text. holder.title.settext(datalist.get(position)); //change image of selected item dynamically. if(selectedposition == position) { //item selected. holder.img.setimageresource(r.drawable.selected); }else{ //other non-selected items. holder.img.setimageresource(r.drawable.dis_selected); } return row; } } }
Comments
Post a Comment