android - Change Image by clicking imageView in getView() of Adapter -
goal: want when ever click image inside list item make changes in
- database
- textview (as number of likes increases)
- change image represent list item liked.
error: whenever click image makes changes in database , increases count of corresponding textview.
but change images of other list items of clicked item.
pleas me following code:
public class populartabrowadapter extends baseadapter { list<apidatamodel> videolist; private activity activity; private static layoutinflater inflater = null; imageloader imageloader; populardatasource datasource; int pos; viewholder holder; listview mlistview; public populartabrowadapter(activity activity, list<apidatamodel> videolist, listview list) { this.activity = activity; this.videolist = videolist; inflater = (layoutinflater) this.activity .getsystemservice(context.layout_inflater_service); imageloader = new imageloader(activity); mlistview = list; this.datasource = new populardatasource(activity); } @override public int getcount() { // todo auto-generated method stub return videolist.size(); } @override public object getitem(int arg0) { // todo auto-generated method stub return videolist.get(arg0); } @override public long getitemid(int arg0) { // todo auto-generated method stub return videolist.get(arg0).getid(); } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub view row = convertview; holder = null; pos = position; if (row == null) { row = inflater.inflate(r.layout.popular_tab_custum_row, parent, false); holder = new viewholder(); holder.descriptiontxt = (textview) row .findviewbyid(r.id.decription_popcustomrow); holder.listtxt = (textview) row .findviewbyid(r.id.list_popcustomrow); holder.imageview = (imageview) row .findviewbyid(r.id.image_popcustomrow); holder.likeimage = (imageview) row .findviewbyid(r.id.likimage_popcustrow); holder.likeimage.setonclicklistener(likeimagelistner); holder.numblike = (textview) row .findviewbyid(r.id.nolik_popcustrow); row.settag(holder); } else { holder = (viewholder) row.gettag(); } holder.descriptiontxt.settext(videolist.get(position).getdecription()); holder.listtxt.settext(videolist.get(position).getlist()); imageloader.displayimage(applicationmain .getyoutubevideoimageurl(videolist.get(position).geturl()), holder.imageview); holder.numblike.settext(string.valueof(videolist.get(position) .getnumberoflikes())); if (videolist.get(position).getlikestatus() == 1) { holder.likeimage.setimageresource(r.drawable.like_done); } return row; } onclicklistener likeimagelistner = new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub viewholder vh = getviewholder(v); int positionvh = mlistview.getpositionforview((view) v.getparent()); if (isnetworkavailable()) { toast.maketext(activity, positionvh + "", toast.length_long) .show(); if (videolist.get(positionvh).getlikestatus() == 0) { hitlike = new hitlike(positionvh, vh.likeimage, vh.numblike); like.execute(); } } } }; public viewholder getviewholder(view v) { if (v.gettag() == null) { return getviewholder((view) v.getparent()); } return (viewholder) v.gettag(); } class hitlike extends asynctask<void, void, string> { imageview image; int position; textview text; public hitlike(int position, imageview image, textview text) { this.position = position; this.image = image; this.text = text; } @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); activity.setprogressbarindeterminatevisibility(true); } @override protected string doinbackground(void... params) { // todo auto-generated method stub servicehandler sh = new servicehandler(); string responce = sh.makeservicecall( applicationmain.instance.getpoplikeapi(), servicehandler.post); log.i("popularrowadapterresponce", responce); return responce; } @override protected void onpostexecute(string result) { // todo auto-generated method stub super.onpostexecute(result); activity.setprogressbarindeterminatevisibility(false); datasource.open(); if (datasource.addlike(videolist.get(position).getid(), videolist .get(position).getnumberoflikes())) { apidatamodel model = new apidatamodel(videolist.get(position) .getlist(), videolist.get(position).getdecription(), videolist.get(position).geturl()); model.setnumberoflikes(videolist.get(position) .getnumberoflikes() + 1); model.setlikestatus(1); videolist.set(position, model); notifydatasetchanged(); image.setimageresource(r.drawable.like_done); } datasource.close(); } } private boolean isnetworkavailable() { connectivitymanager connectivitymanager = (connectivitymanager) activity .getsystemservice(context.connectivity_service); networkinfo activenetworkinfo = connectivitymanager .getactivenetworkinfo(); return activenetworkinfo != null && activenetworkinfo.isconnected(); } static class viewholder { textview descriptiontxt; textview listtxt; imageview imageview; imageview likeimage; textview numblike; } }
thanks :)
image.setimageresource(r.drawable.like_done);
and set perticular position image change because of in getview , there viewholder used. have
1) create temp array selected image position.
2) put loop in getview , upto temp size if getview position==temp[i]
image.setimageresource(r.drawable.like_done);
else
image.setimageresource(r.drawable.default);
thats it...
Comments
Post a Comment