android - Updating Listview item -
i need update each item row after added listview
.i using abc_slide_in_bottom
animation
all want when item added listview
after sliding bottom previous item textview
(having text value) should change imageview
.
mainactivity:
protected void oncreate(bundle savedinstancestate) { dataobj=new data(); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mylistview=(listview)findviewbyid(r.id.lv_preflight); gobtn=(button)findviewbyid(r.id.btn_go); context=this.getapplicationcontext(); setdata();//to add first item dataobj customadapter=new datalistadapter(dataobj,context,mainactivity.this); mylistview.setadapter(customadapter); layoutanimationcontroller lac = new layoutanimationcontroller(animationutils.loadanimation(getapplicationcontext(), r.anim.abc_fade_in), 2f); mylistview.setlayoutanimation(lac); }
i calling onupdate whenever new item added listview,to update old item. onupdate function in mainactivity
public void onupdate(int position) { view v=mylistview.getchildat(position); if(v!=null) { textview titletv=(textview)v.findviewbyid(r.id.tv_itemname); textview statustv=(textview)v.findviewbyid(r.id.tv_itemstatus); imageview imageiv=(imageview)v.findviewbyid(r.id.iv_icon); if(statustv.getvisibility()==view.visible) { log.e(datalistadapter.tag,"pos "+position+" title: "+titletv.gettext()); statustv.setvisibility(view.invisible); imageiv.setvisibility(view.visible); } } dataobj.itemname.add(items[position+1]); dataobj.status.add("checking..."); dataobj.showimg.add(false); customadapter=new datalistadapter(dataobj ,context,mainactivity.this); customadapter.notifydatasetchanged(); count++; }
here have added custom adapter listview. custom adapter's getview method:
public view getview(final int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) this.context.getsystemservice(context.layout_inflater_service); view row = convertview; if (row == null) { row = inflater.inflate(r.layout.preflight_row_layout, parent, false); holder = new viewholder(); row.settag(holder); } else { holder = (viewholder) row.gettag(); } holder.title = (textview) row.findviewbyid(r.id.tv_itemname); holder.status = (textview) row.findviewbyid(r.id.tv_itemstatus); holder.img=(imageview)row.findviewbyid(r.id.iv_icon); holder.title.settext(title.get(position)); holder.status.settext(status.get(position)); holder.img.setimageresource(r.drawable.tick_image); if (position+1==this.title.size())//when new item added call onupdate { update.onupdate(position);//customlistner's function called } return (row); }
the row_layout.xml
contains 2 textview
s item title, item status(as "checking....") , imageview
. trying update previous item in listview
"checking...." textview
imageview
when new item gets added listview
. requirement kind of matches this link not able update old item,moreover items added randomly listview
get reference animation
, set listener on
animation anim = animationutils.loadanimation(getapplicationcontext(),r.anim.abc_slide_in_bottom); anim.setanimationlistener(new animationlistener() { @override public void onanimationstart(animation arg0) {} @override public void onanimationrepeat(animation arg0) {} @override public void onanimationend(animation arg0) { // here animation has ended // call notifydatasetchanged here, before add // item list update needs true; after // getview method false; } });
now how thinking put logic that, logic you, when item added listview
added data object backs it, if yours arraylist
last item in list, in getview
can check last item in list
, add modifications it, modifications here in getview
method update particular item, eliminate updating of last item everytime, can add boolean
check if item has been added, or can put boolean
field in data object check , toggle false change view
edit 1 custom data object
public class data{ string title,nxtstatus,prevstatus; boolean update; //any additional stuff can go here depends on }
then use arraylist
, arraylist<data> data = new arraylist<data>();
create class view
s public class myviews { textview title, status,prevstatus; imageview previmg;}
in getview
method
public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) this.context.getsystemservice(context.layout_inflater_service); view row = convertview; myviews myv; // might required initialize data data = getitem(position); //getitem super class's method if(row == null){ row = inflater.inflate(r.layout.row_layout, parent, false); myv = new myviews(); myv.title = (textview) row.findviewbyid(r.id.tv_itemname); myv.status = (textview) row.findviewbyid(r.id.tv_itemstatus); myv.previmg=(imageview)newrow.findviewbyid(r.id.iv_icon); myv.previmg.setvisibiltiy(view.gone); title.settext(data.title); status.settext(data.status); row.settag(myv);}else{myv = row.gettag(); //some casting go on here } if(data.update){ // if update true means added myv.prevstatus.setvisibility(view.gone); myv.previmg.setvisibility(view.visible); data.update = false; // next pass view not updated } //then return row; return row;}
i think that's enough going, go animation code on top pretty basic right?
Comments
Post a Comment