Onclick image result not accurate

Hi guys, my code is work smoothly. But the problem is, the result is not accurate. If i click one time, its detect 2 or 3. What is the problem? Any ideas to solve the problem?

 [Activity (Label = "MyVote", MainLauncher = true, Icon = "@drawable/icon")]
public class MyVote_MainActivityPO : Activity
{
    MyVoteMAPOAdapter mVoteAdapter;
    List<ListData> listData;
    ListView lvMVPLPosterList;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Create your application here
        SetContentView (Resource.Layout.Main);

        lvMVPLPosterList = (ListView)FindViewById (Resource.Id.lvMVPLPosterList);

        listData = new List<ListData> () {
            new ListData () { poster = Resource.Drawable.voteex_poster1, like = Resource.Drawable.vote_like_50px },
            new ListData () { poster = Resource.Drawable.voteex_poster2, like = Resource.Drawable.vote_like_50px },
            new ListData () { poster = Resource.Drawable.voteex_poster3, like = Resource.Drawable.vote_like_50px },
            new ListData () { poster = Resource.Drawable.voteex_poster4, like = Resource.Drawable.vote_like_50px },
        };

        mVoteAdapter = new MyVoteMAPOAdapter (this, listData);
        lvMVPLPosterList.Adapter = mVoteAdapter;
    }

    public class MyVoteMAPOAdapter : BaseAdapter<ListData>
    {
        List<ListData> mData = new List<ListData>();
        Activity context;
        int votevalue = -1;
        ViewHolder holder;
        LayoutInflater mLayoutInflater;

        public MyVoteMAPOAdapter (Activity context, List<ListData> mData) : base()
        {
            this.context = context;
            this.mData = mData;
            mLayoutInflater = (LayoutInflater)context.GetSystemService (Context.LayoutInflaterService);

        }
        public override long GetItemId(int position)
        {
            return 0;
        }

        public override ListData this[int position]
        {
            get { return mData[position]; }
        }

        public override int Count
        {
            get { return mData.Count; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = mData[position];
            View view = convertView;

            if (convertView == null) {  // no view to re-use, create new
                view = mLayoutInflater.Inflate (Resource.Layout.FragmentVote, null);
                holder = new ViewHolder ();
                holder.IVPoster = view.FindViewById <ImageView> (Resource.Id.ivMVPDGCPosterImage);
                holder.IVLike = view.FindViewById <ImageView> (Resource.Id.imageView1);
                view.Tag = holder;
            }else{
                holder = view.Tag as ViewHolder;
            }
            holder.IVPoster.SetImageResource (item.poster);
            holder.IVLike.SetImageResource (item.like);

            holder.IVLike.Clickable = true;
            holder.IVLike.Click += IVLike_Click;

            return view;
        }

        public void IVLike_Click(object sender, EventArgs e){

            if(votevalue == -1){
                AlertDialog.Builder builder = new AlertDialog.Builder (context);
                AlertDialog alertDialog = builder.Create ();
                alertDialog.SetTitle ("Vote");
                alertDialog.SetMessage ("Please Vote");
                alertDialog.SetButton ("OK", (s, ev) =>
                    {

                    });
                alertDialog.Show ();
                Console.WriteLine ("Like");
                votevalue = 1;
            }else if(votevalue == 1){

                votevalue = -1;
                Console.WriteLine ("DisLike");
            }
        }
    }

    public class ViewHolder : Java.Lang.Object
    {
        public ImageView IVPoster { get; set;}
        public ImageView IVLike { get; set;}
    }

    public class ListData
    {
        public int poster { get; set;}
        public int like { get; set;}
    }
}

Posts

  • JoeRock11JoeRock11 USMember
    edited March 2015

    My guess would be since you are registering your click event inside of the GetView method then when Android reuses your holder it's also subscribing another method to your already created event. Try putting the line holder.IVLike.Click += IVLike_Click; inside of the condition where convertView is checked for null. So something like.

    if (convertView == null) {  // no view to re-use, create new
                    view = mLayoutInflater.Inflate (Resource.Layout.FragmentVote, null);
                    holder = new ViewHolder ();
                    holder.IVPoster = view.FindViewById <ImageView> (Resource.Id.ivMVPDGCPosterImage);
                    holder.IVLike = view.FindViewById <ImageView> (Resource.Id.imageView1);
                    view.Tag = holder;
            holder.IVLike.Click += IVLike_Click;
    }
    
  • Ah okay. Its work!! Thanks joe. And 1 more thing, how can I retrieve the image position, after i click the image?
    so i have try something like this :

        ViewHolder holder = (ViewHolder)v.Tag;
                int id = (int)holder;
    

    But it doesnt work.

Sign In or Register to comment.