Forum Xamarin.Android

Listview populating incorrect data when scroll up and down

scubby3scubby3 Member ✭✭

For a entire now i have been trying to get my listview to work according to how i wanted it to with no luck what so ever. I am no pro at xamarin android but but i am challenging my self to learn by taking on very hard task which help me to learn in a better way.

I have a custom listview adapter which populate from an Api with textview and imageview.
the data that return from the api is of a timeline like facebook or instagram.
my problem is when a user click the like button it send information of that particular view back to the server which is control by and id.
when the user scroll up or down and click another like button it send the wrong id back to the server which causes the first step to override.
each row of data is of different id so if the user click each like button it should write to the database base on the id of that row.

some how when user scroll up and down the position of first view seem to be the same position of other view which poses a problem
here is my code and screen shot of what i am taking about.

`class FeedAdapter : BaseAdapter
{
Activity context;
List list;
public FeedAdapter(Activity _context, List _list) : base()
{
this.context = _context;
this.list = _list;
}
public List GetList()
{
return list;
}

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

    public override long GetItemId(int position)
    {
        return list[position].Feed_Id;
    }

    public override Feed this[int position]
    {
        get { return list[position]; }
    }


    public override View GetView(int position, View convertView, ViewGroup parent)
    {

        var view = convertView;
        FeedAdapterViewHolder holder = null;
        Feed item = this[position];

        if (view != null)
        {
            holder = view.Tag as FeedAdapterViewHolder;
        }
        else 
        {
            holder = new FeedAdapterViewHolder();
            var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
            view = inflater.Inflate(Resource.Layout.feedRows, parent, false);

            holder.UserName = view.FindViewById<TextView>(Resource.Id.txtName);
            holder.UserImg = view.FindViewById<ImageView>(Resource.Id.imgUser);
            holder.Img = view.FindViewById<ImageView>(Resource.Id.imgPhoto);
            holder.ImgValue = view.FindViewById<ImageView>(Resource.Id.btnValues);
            holder.ImgLikes = view.FindViewById<ImageView>(Resource.Id.btnLikes);
            holder.ShowCommentBox = view.FindViewById<ImageView>(Resource.Id.btnComment);
            holder.TextPost = view.FindViewById<TextView>(Resource.Id.txtPost);
            holder.TextName = view.FindViewById<TextView>(Resource.Id.txtName);
            holder.txtComments = view.FindViewById<TextView>(Resource.Id.txtComments);
            holder.txtValues = view.FindViewById<TextView>(Resource.Id.txtValues);
            holder.txtLikes = view.FindViewById<TextView>(Resource.Id.txtLikes);
            holder.TxtTime = view.FindViewById<TextView>(Resource.Id.txtTime);
            view.Tag = holder;


        }



        string api = list[position].Api_status.ToString();
        if (api == "200")
        {
            Glide.With(context).Load(list[position].Photo).Into(holder.Img); //this.photo can also be used

            holder.txtComments.Text = list[position].Comment_Count;
            holder.TextName.Text = list[position].Fullname;
            holder.txtValues.Text = list[position].Feed_Values;
            holder.TxtTime.Text = list[position].Feed_time;

            if (list[position].Feed_Text != "")
            {
                holder.TextPost.Text = System.Net.WebUtility.HtmlDecode(list[position].Feed_Text);
            }

            var like = Resource.Drawable.ic_like;
            var likes = Resource.Drawable.ic_likes;

            if (list[position].User_like.ToString() == "100")
            {
                Glide.With(context).Load(likes).Into(holder.ImgLikes);
                holder.txtLikes.Text = list[position].Likes_Count;
            }
            else if (list[position].User_like.ToString() == "200")
            {
                Glide.With(context).Load(like).Into(holder.ImgLikes);
                holder.txtLikes.Text = list[position].Likes_Count;
            }



            if (!holder.UserImg.HasOnClickListeners)
            {
                holder.UserImg.Click += (sender, args) =>
                {
                    Intent i = new Intent(context, typeof(ViewUserProfile));
                    i.PutExtra("UserId", list[position].User_id.ToString());
                    context.StartActivity(i);
                };
            }

            if (!holder.ShowCommentBox.HasOnClickListeners)
            {
                holder.ShowCommentBox.Click += (sender, args) =>
                {
                    int pos = (int)holder.ShowCommentBox.Tag;
                    Intent i = new Intent(context, typeof(CommentsActivity));
                    i.PutExtra("PostId", list[position].Feed_Id);
                    context.StartActivity(i);
                };
            }

            if (!holder.ImgLikes.HasOnClickListeners)
            {
                holder.ImgLikes.Click += (sender, args) =>
                {
                    int x = position;
                    var client = new RestClient(settings.Website);
                    var request = new RestRequest("app_api.php?type=feed_likes", Method.POST);

                    request.AddParameter("userid", Helpers.Settings.Userid);
                    request.AddParameter("feedid", list[position].Feed_Id);
                    var response = client.Execute<LikesClass>(request);
                    if (response.Data.Api_status == "200")
                    {
                        if (response.Data.Api_code == "100")// delete post
                        {
                            Glide.With(context).Load(Resource.Drawable.ic_like).Into(holder.ImgLikes);
                            holder.txtLikes.Text = response.Data.Likes;
                        }
                        else if (response.Data.Api_code == "300") // Like post
                        {
                            Glide.With(context).Load(Resource.Drawable.ic_likes).Into(holder.ImgLikes);
                            holder.txtLikes.Text = response.Data.Likes;
                        }
                    }
                    else if (response.Data.Api_status == "400")
                    {
                        Toast.MakeText(context, response.Data.Errors_text.ToString(), ToastLength.Long).Show();
                    }

                };
            }



            if (!holder.ImgValue.HasOnClickListeners)
            {
                holder.ImgValue.Click += (sender, args) =>
                {
                    holder.ImgValue.SetImageResource(Resource.Drawable.ic_values_1);
                };
            }
        }
        else if (api == "400")
        {
            Toast.MakeText(context, list[position].Errors_text.ToString(), ToastLength.Long).Show();
        }


        return view;
    }


    private class FeedAdapterViewHolder : Java.Lang.Object
    {
        public TextView UserName { get; set; }
        public ImageView UserImg { get; set; }
        public ImageView Img { get; set; }
        public ImageView ImgValue { get; set; }
        public ImageView ImgLikes { get; set; }
        public ImageView ShowCommentBox { get; set; }
        public TextView TextPost { get; set; }
        public TextView TextName { get; set; }
        public TextView txtComments { get; set; }
        public TextView txtValues { get; set; }
        public TextView txtLikes { get; set; }
        public TextView TxtTime { get; set; }
    }

}![](https://us.v-cdn.net/5019960/uploads/editor/yp/sgka2rvggq5n.png "")


`

Best Answer

  • scubby3scubby3 Member ✭✭
    Accepted Answer

    So i have mange to invoke the click even to my fragment where list item is displayed and i started to get the correct information needed.

    Just need to figure out how to update the ui after button click inside the dadapter.

Answers

  • scubby3scubby3 Member ✭✭

    As stated above if you notice in both image like button one selected and one is not.
    if i select the one that is not then its either it remove the first action from the database or it duplicate.
    So what i want is each view have an id came from the database and if each row is click i want the id of each row which came from the data to return back to the database but instead the positions are mixing up the ids.

    This would be awesome if i can get some help on fixing this.

  • scubby3scubby3 Member ✭✭
    Accepted Answer

    So i have mange to invoke the click even to my fragment where list item is displayed and i started to get the correct information needed.

    Just need to figure out how to update the ui after button click inside the dadapter.

  • Please add these lines in your adapter class. Hope this code helps you to resolve your issue.Thanks.

    @Override
    public int getViewTypeCount() {

    return getCount();
    }

    @Override
    public int getItemViewType(int position) {

    return position;
    }

Sign In or Register to comment.