java - OnClickListener doesn't work in custom listview -
i seek utilize onclicklistener
in custom listview,but when textview clicked (onclicklistener run) it's runing code on several textview in listview! see holder.tvcomment.setonclicklistener
in listadapter.class
updated:
import java.text.parseexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.date; import java.util.list; import com.skyline.jimmy.widget.liketoast; import com.skyline.jimmy.widget.mytoast; import android.annotation.suppresslint; import android.app.actionbar; import android.app.activity; import android.content.clipdata; import android.content.context; import android.content.intent; import android.net.uri; //import android.text.clipboardmanager; import android.content.clipboardmanager; import android.graphics.color; import android.graphics.typeface; import android.graphics.drawable.drawable; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.view.onclicklistener; import android.view.window; import android.view.windowmanager; import android.widget.baseadapter; import android.widget.button; import android.widget.imagebutton; import android.widget.imageview; import android.widget.ratingbar; import android.widget.textview; import android.widget.toast; public class votelistadapter extends baseadapter { protected static final string clipboard_service = null; private final string tag = "*** votelistadapter ***"; private context context; private arraylist<vote> votes; int showhidebtn = 0; public votelistadapter(context context,list<vote> votelist) { this.context =context; this.votes =(arraylist<vote>) votelist; } @override public int getcount() { homecoming votes.size(); } @override public object getitem(int position) { homecoming votes.get(position); } @override public long getitemid(int position) { homecoming position; } @override public view getview(final int position, view convertview, viewgroup parent) { final viewholder holder; if (convertview == null) { holder = new viewholder(); convertview = layoutinflater.from(context).inflate(r.layout.row, null); holder.tvname = (textview) convertview.findviewbyid(r.id.user); holder.tvcomment = (textview) convertview.findviewbyid(r.id.tvcomment); holder.tvdate = (textview) convertview.findviewbyid(r.id.tdate); holder.tvlikes = (textview) convertview.findviewbyid(r.id.tvlikes); holder.ratingbar = (ratingbar)convertview.findviewbyid(r.id.ratingbar); holder.share = (imagebutton) convertview.findviewbyid(r.id.sharebtn); holder.copyjoke = (imagebutton) convertview.findviewbyid(r.id.copyjoke); holder.btnlike = (button) convertview.findviewbyid(r.id.likebtn); holder.btnliked = (button) convertview.findviewbyid(r.id.likedbtn); holder.btns = (imagebutton) convertview.findviewbyid(r.id.btns); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } typeface yaghut=typeface.createfromasset(context.getassets(), "font/far_casablanca.ttf"); string likes = integer.parseint(votes.get(position).getrate()) + " likes"; holder.tvname.settext(votes.get(position).getname()); holder.tvcomment.settext(votes.get(position).getcomment()); holder.tvdate.settext(getformateddate(votes.get(position).getpublishdate())); holder.tvlikes.settext(likes); holder.ratingbar.setrating(integer.parseint(votes.get(position).getrate())); holder.tvcomment.settypeface(yaghut); if (convertview == null) { holder.tvcomment.setonclicklistener( new onclicklistener() { int clicks = 0; @override public void onclick(view v) { clicks = clicks + 1; if(clicks == 1){ }else if(clicks == 2) { liketoast.maketext(context, "liked" , toast.length_short).show(); holder.btnlike.settext("liked"); drawable image = context.getresources().getdrawable( r.drawable.ic_action_liked ); int hlike = image.getintrinsicheight(); int wlike = image.getintrinsicwidth(); image.setbounds( 0, 0, wlike, hlike ); holder.btnlike.setcompounddrawables( image, null, null, null ); //holder.btnlikes.setbackgrounddrawable(r.drawable.ic_action_liked); } else{ clicks = 2; } } }); convertview.settag(holder); } holder.btns.setonclicklistener( new onclicklistener() { @override public void onclick(view v) { if(showhidebtn == 1){ holder.share.setvisibility(view.invisible); holder.btns.setvisibility(view.visible); holder.copyjoke.setvisibility(view.invisible); showhidebtn = 0; } else { holder.share.setvisibility(view.visible); holder.btns.setvisibility(view.visible); holder.copyjoke.setvisibility(view.visible); showhidebtn = 1; } } }); holder.copyjoke.setonclicklistener( new onclicklistener() { @override public void onclick(view v) { android.text.clipboardmanager clipboard = (android.text.clipboardmanager) context.getsystemservice(context.clipboard_service); string joke = votes.get(position).getcomment(); clipboard.settext(joke); toast toast3 = mytoast.maketext(context, "جک مورد نظر کپی شد", toast.length_short); toast3.show(); } }); holder.share.setonclicklistener( new onclicklistener() { @override public void onclick(view v) { /* intent = new intent(intent.action_send); i.settype("text/plain"); i.putextra(intent.extra_text,votes.get(position).getcomment()); seek { context.startactivity(intent.createchooser(i, "share")); } grab (android.content.activitynotfoundexception ex) { ex.printstacktrace(); } */ toast toast = mytoast.maketext(context, "این قسمت در نسخه بتا در حال طراحی است", toast.length_long); toast.show(); toast toast2 = mytoast.maketext(context, "منتظر آپدیت باشید", toast.length_short); toast2.show(); } }); homecoming convertview; } private string getformateddate(string date) { string mydate = null; seek { date olddate = new simpledateformat("yy-mm-dd hh:mm:ss").parse(date); mydate = new simpledateformat("dd mmm yyyy").format(olddate); } grab (parseexception e) { mydate = ""; e.printstacktrace(); } homecoming mydate; } static class viewholder { imagebutton copyjoke; imagebutton share; imagebutton btns; button btnlike; button btnliked; textview tvname; textview tvlikes; textview tvcomment; textview tvdate; ratingbar ratingbar; } }
write setonclicklistener code if condition
if (convertview == null) { ..... ..... // write here holder.tvcomment.setonclicklistener convertview.settag(holder); }
and before line:
convertview.settag(holder);
your total adapter this:
import java.text.parseexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.date; import java.util.list; import com.skyline.jimmy.widget.liketoast; import com.skyline.jimmy.widget.mytoast; import android.annotation.suppresslint; import android.app.actionbar; import android.app.activity; import android.content.clipdata; import android.content.context; import android.content.intent; import android.net.uri; //import android.text.clipboardmanager; import android.content.clipboardmanager; import android.graphics.color; import android.graphics.typeface; import android.graphics.drawable.drawable; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.view.onclicklistener; import android.view.window; import android.view.windowmanager; import android.widget.baseadapter; import android.widget.button; import android.widget.imagebutton; import android.widget.imageview; import android.widget.ratingbar; import android.widget.textview; import android.widget.toast; public class votelistadapter extends baseadapter { viewholder holder; protected static final string clipboard_service = null; private final string tag = "*** votelistadapter ***"; private context context; private arraylist<vote> votes; int showhidebtn = 0; public votelistadapter(context context,list<vote> votelist) { this.context =context; this.votes =(arraylist<vote>) votelist; } @override public int getcount() { homecoming votes.size(); } @override public object getitem(int position) { homecoming votes.get(position); } @override public long getitemid(int position) { homecoming position; } @override public view getview(final int position, view convertview, viewgroup parent) { if (convertview == null) { holder = new viewholder(); convertview = layoutinflater.from(context).inflate(r.layout.row, null); holder.tvname = (textview) convertview.findviewbyid(r.id.user); holder.tvcomment = (textview) convertview.findviewbyid(r.id.tvcomment); holder.tvdate = (textview) convertview.findviewbyid(r.id.tdate); holder.tvlikes = (textview) convertview.findviewbyid(r.id.tvlikes); holder.ratingbar = (ratingbar)convertview.findviewbyid(r.id.ratingbar); holder.share = (imagebutton) convertview.findviewbyid(r.id.sharebtn); holder.copyjoke = (imagebutton) convertview.findviewbyid(r.id.copyjoke); holder.btnlike = (button) convertview.findviewbyid(r.id.likebtn); holder.btnliked = (button) convertview.findviewbyid(r.id.likedbtn); holder.btns = (imagebutton) convertview.findviewbyid(r.id.btns); holder.btns.setonclicklistener( new onclicklistener() { @override public void onclick(view v) { if(showhidebtn == 1){ holder.share.setvisibility(view.invisible); holder.btns.setvisibility(view.visible); holder.copyjoke.setvisibility(view.invisible); showhidebtn = 0; } else { holder.share.setvisibility(view.visible); holder.btns.setvisibility(view.visible); holder.copyjoke.setvisibility(view.visible); showhidebtn = 1; } } }); holder.copyjoke.setonclicklistener( new onclicklistener() { @override public void onclick(view v) { android.text.clipboardmanager clipboard = (android.text.clipboardmanager) context.getsystemservice(context.clipboard_service); string joke = votes.get(position).getcomment(); clipboard.settext(joke); toast toast3 = mytoast.maketext(context, "جک مورد نظر کپی شد", toast.length_short); toast3.show(); } }); holder.share.setonclicklistener( new onclicklistener() { @override public void onclick(view v) { /* intent = new intent(intent.action_send); i.settype("text/plain"); i.putextra(intent.extra_text,votes.get(position).getcomment()); seek { context.startactivity(intent.createchooser(i, "share")); } grab (android.content.activitynotfoundexception ex) { ex.printstacktrace(); } */ toast toast = mytoast.maketext(context, "این قسمت در نسخه بتا در حال طراحی است", toast.length_long); toast.show(); toast toast2 = mytoast.maketext(context, "منتظر آپدیت باشید", toast.length_short); toast2.show(); } }); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } typeface yaghut=typeface.createfromasset(context.getassets(), "font/far_casablanca.ttf"); string likes = integer.parseint(votes.get(position).getrate()) + " likes"; holder.tvname.settext(votes.get(position).getname()); holder.tvcomment.settext(votes.get(position).getcomment()); holder.tvdate.settext(getformateddate(votes.get(position).getpublishdate())); holder.tvlikes.settext(likes); holder.ratingbar.setrating(integer.parseint(votes.get(position).getrate())); holder.tvcomment.settypeface(yaghut); homecoming convertview; } private string getformateddate(string date) { string mydate = null; seek { date olddate = new simpledateformat("yy-mm-dd hh:mm:ss").parse(date); mydate = new simpledateformat("dd mmm yyyy").format(olddate); } grab (parseexception e) { mydate = ""; e.printstacktrace(); } homecoming mydate; } static class viewholder { imagebutton copyjoke; imagebutton share; imagebutton btns; button btnlike; button btnliked; textview tvname; textview tvlikes; textview tvcomment; textview tvdate; ratingbar ratingbar; } }
java android listview android-listview
No comments:
Post a Comment