android - Issue on changing the resource in imageView when that imageView is pressed -
i trying write application change image resource, when imageview pressed.
i referred this link.
i used both setontouchlistener
, setonclicklistener
in code. in both, logs. but, image didn't change. have given code below:
imageview more; more = (imageview) row.findviewbyid(r.id.imageview1); more.setimageresource(r.drawable.ic_more_1);
setontouchlistener
is:
more.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub log.d(tag,"event type "+event+" "+ event.getaction()); switch (event.getaction()) { case motionevent.action_down: more.setimageresource(r.drawable.ic_more_2); break; case motionevent.action_up: more.setimageresource(r.drawable.ic_more_1); break; } return false; } });
my setonclicklistener
is:
more.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (!buttonclicked) { log.d(tag, "on image click..... false" + buttonclicked); buttonclicked = true; more.setimageresource(r.drawable.ic_more_2); } if (buttonclicked) { log.d(tag, "on image click..... true" + buttonclicked); buttonclicked = false; more.setimageresource(r.drawable.ic_more_1); } // more.setimageresource(r.drawable.ic_more_2); // todo auto-generated method stub /* log.d(tag, "on image click..... " + pos); * if(listdata.getposition() == subscribe) { * more.setimageresource(r.drawable.ic_more_2); * //more.setimageresource(r.drawable.ic_more_1); log.d(tag, * "for subscribe..................."); intent = new * intent(getcontext(), episodeactivity.class); * intent.putextra("url", strurl); * getcontext().startactivity(intent); * * } else { // todo rewrite adding faviourite items * log.d(tag, "for episode..................."); } */ } });
please point out mistake have done.
thank in advance!!!
note: since want changes in listview
listitem images
, using code inside custom adapter
update:
my adapter:
public class customadapterforsubcribe extends arrayadapter<listdata> { public final string tag = "customadapterforsubcribe"; context context; int layoutresourceid; boolean buttonclicked = false; layoutinflater inflater; textview title, desc; imageview more, fav, imagerightsubscribe, imagerightepisode; intent intent; public static int subscribe = 1; public static int episode = 0; private string strurl = "http://timesofindia.feedsportal.com/c/33039/f/533916/index.rss"; private string imageurlepisode = "http://news.bbcimg.co.uk/media/images/66996000/jpg/_66996549_66996528.jpg"; public customadapterforsubcribe(context context, int layoutresourceid, list<listdata> objects, int option) { // todo auto-generated constructor stub super(context, layoutresourceid, objects); this.context = context; this.layoutresourceid = layoutresourceid; this.inflater = layoutinflater.from(context); } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub final int pos = position; view row = convertview; if (row == null) { layoutinflater inflater = layoutinflater.from(getcontext()); row = inflater.inflate(r.layout.episode_row_view, parent, false); } final listdata listdata = getitem(position); title = (textview) row.findviewbyid(r.id.title); desc = (textview) row.findviewbyid(r.id.description); more = (imageview) row.findviewbyid(r.id.imageview1); if (listdata.getposition() == subscribe) { more.setimageresource(r.drawable.ic_more_1); } else { more.setimageresource(r.drawable.ic_favourite_1); } // more.setimageresource(r.drawable.ic_more_1); imagerightepisode = (imageview) row.findviewbyid(r.id.image); imagerightepisode.settag(imageurlepisode); new downloadimagestask().execute(imagerightepisode); more.settag(new integer(position)); if (listdata.getposition() == subscribe) { log.d(tag, "<<<<.....in option.....>>>>"); desc.setvisibility(view.gone); // title.setgravity(gravity.center_horizontal); // title.setgravity(gravity.center_vertical); title.setgravity(gravity.center); title.settextsize(16); title.settypeface(typeface.default_bold); } title.settext(listdata.gettitle()); desc.settext(listdata.getdesc()); // more.setontouchlistener(new view.ontouchlistener() { // // @override // public boolean ontouch(view v, motionevent event) { // // todo auto-generated method stub // log.d(tag, "event type " + event + " " + event.getaction()); // if (listdata.getposition() == subscribe) { // switch (event.getaction()) { // // case motionevent.action_down: // log.d(tag, "subscribe...image press releasing...."); // more.setimageresource(r.drawable.ic_more_1); // more.invalidate(); // break; // case motionevent.action_up: // log.d(tag, "subscribe... image pressing...."); // more.setimageresource(r.drawable.ic_more_2); // break; // } // } // // return false; // } // }); more.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (listdata.getposition() == episode) { if (!buttonclicked) { log.d(tag, "on image click..... false" + pos); more.setimageresource(r.drawable.ic_favourite_2); buttonclicked = true; } else if (buttonclicked) { log.d(tag, "on image click..... " + buttonclicked); more.setimageresource(r.drawable.ic_favourite_1); buttonclicked = false; } } // more.setimageresource(r.drawable.ic_more_2); // todo auto-generated method stub else if (listdata.getposition() == subscribe) { log.d(tag, "on image click..... " + pos); log.d(tag, "for subscribe..................."); intent = new intent(getcontext(), episodeactivity.class); intent.putextra("url", strurl); getcontext().startactivity(intent); } else { // todo rewrite adding faviourite items log.d(tag, "for episode..................."); } } }); return row; }
even changed in code, touchclicklistener didn't work image
i used holder
, changed if
condition instead of switch
case
my working code is:
holder.more.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (listdata.getposition() == subscribe) { if(event.getaction() == motionevent.action_up){ log.d(tag,"releasing....."); holder.fav.setimageresource(r.drawable.ic_more_1); } if(event.getaction() == motionevent.action_down){ log.d(tag,"pressing....."); holder.fav.setimageresource(r.drawable.ic_more_2); } } return false; } });