• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Double items in recyclerview

I have list and I filtered it using this code :
Code:
            var data = SampleData.SampleDataItems();
            List<SampleData> filteredSavings = data.Where(item => item.AccountCode.Equals("Savings")).ToList();
                listDataChild.Add(listDataHeader[0], filteredSavings);
                List<SampleData> filteredLoans = data.Where(item => item.AccountCode.Equals("Loans")).ToList();
                listDataChild.Add(listDataHeader[1], filteredLoans);

The data pass properly but in recyclerView the items are repeating:
 

Attachments

  • Capture.PNG
    Capture.PNG
    34.8 KB · Views: 146
Can you please provide more information as to what the problem is? Like, post a bigger picture, give a background of what language you are using as it seems to be like C#.

It's Kotlin. But yes, not enough information given to answer the question, and the code fragment is too small.
 
It's Kotlin. But yes, not enough information given to answer the question, and the code fragment is too small.

Hi sorry for late reply. It's Xamarin.Android.

OnCreateView in my fragment
Code:
 listDataHeader = new List<ExpandedMenuModel>();
            listDataChild = new Dictionary<ExpandedMenuModel, List<SampleData>>();

            ExpandedMenuModel item1 = new ExpandedMenuModel();
            item1.Title = "Savings";
            listDataHeader.Add(item1);

            ExpandedMenuModel item2 = new ExpandedMenuModel();
            item2.Title = "Expenses";
            listDataHeader.Add(item2);

listDataChild.Add(listDataHeader[0], SampleData.FilteredDataSavings());
            listDataChild.Add(listDataHeader[1], SampleData.FilteredDataExpenses());
menuAdapter = new SampleAccountExpandable(this.Activity, listDataHeader, listDataChild, expandableMyAccount);
            expandableMyAccount.SetAdapter(menuAdapter);
            expandableMyAccount.ExpandGroup(0);
            expandableMyAccount.GroupClick += ExpandableMyAccount_GroupClick;

My BaseExpandableListAdapter
Code:
 public SampleAccountExpandable(Activity context, List<ExpandedMenuModel> listDataHeader, Dictionary<ExpandedMenuModel, List<SampleData>> listChildData, ExpandableListView mView)
        {
            _context = context;
            _listDataHeader = listDataHeader;
            _listDataChild = listChildData;
            expandList = mView;
        }
public override int GroupCount
        {
            get
            {
                return _listDataHeader.Count;
            }
        }

        public override bool HasStableIds
        {
            get
            {
                return true;
            }
        }

        public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
        {
            throw new NotImplementedException();
        }


        protected List<SampleData> GetSampleData(int groupPosition, int childPosition)
        {
            var sampleData = _listDataChild[_listDataHeader[groupPosition]];
            return sampleData;
        }
  public override long GetChildId(int groupPosition, int childPosition)
        {
            return childPosition;
        }

        public override int GetChildrenCount(int groupPosition)
        {
            return _listDataChild[_listDataHeader[groupPosition]].Count();
        }
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
        {
             childValue = GetSampleData(groupPosition, childPosition);
            View view = convertView;
            if (view == null)
            {
                //convertView = _context.LayoutInflater.Inflate(Resource.Layout.list_expanda_child, parent, false);
                view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.list_expanda_child, parent, false);
            }

             recyclerView = (RecyclerView)view.FindViewById(Resource.Id.recyclerViewAccount);

            accountLayoutManager = new LinearLayoutManager(_context);
            recyclerView.SetLayoutManager(accountLayoutManager);
            accountRecycler = new SampleAccountRecycler(childValue);
            accountRecycler.ItemClick += OnItemClick;
            recyclerView.SetAdapter(accountRecycler);

            return view;
        }
  public override Java.Lang.Object GetGroup(int groupPosition)
        {
            return new JavaObjectWrapper<ExpandedMenuModel>() { Obj = _listDataHeader[groupPosition] };
        }

        public override long GetGroupId(int groupPosition)
        {
            return groupPosition;
        }

        public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
        {
            ExpandedMenuModel headerTitle = _listDataHeader[groupPosition];
            if (convertView == null)
            {
                convertView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.list_expanda_header, parent, false);
            }
            //convertView = convertView ?? _context.LayoutInflater.Inflate(Resource.Layout.list_expanda_header, parent, false);
            TextView lblTitle = (TextView)convertView.FindViewById(Resource.Id.listHeader);
            lblTitle.Text = headerTitle.Title;
         
            return convertView;
        }

        public override bool IsChildSelectable(int groupPosition, int childPosition)
        {
            return true;
        }


        public class JavaObjectWrapper<T> : Java.Lang.Object
        {
            public T Obj { get; set; }
        }

My RecyclerAdapter
Code:
public SampleAccountRecycler(List<SampleData> accounts)
        {
            account = new List<SampleData>();
            this.account = accounts;
        }
        public override int ItemCount
        {
            get
            {
                return account.Count();
            }
        }


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

        public override int GetItemViewType(int position)
        {
            return position;
        }
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            AccountViewHolder viewHolder = holder as AccountViewHolder;

            viewHolder.txtTypeAccount.Text = account[position].Type;
            viewHolder.txtAccountNumber.Text = account[position].Number;
            viewHolder.txtAccountAmount.Text = account[position].Amount;
         
        }
  public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            View view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.RecylerModelAccount, parent, false);
            AccountViewHolder viewHolder = new AccountViewHolder(view, OnClick);
            return viewHolder;
        }
        internal class AccountViewHolder : RecyclerView.ViewHolder
        {
            public TextView txtTypeAccount;
            public TextView txtAccountNumber;
            public TextView txtAccountAmount;
            public AccountViewHolder(View itemView, Action<int> listener) : base(itemView)
            {
                txtTypeAccount = itemView.FindViewById<TextView>(Resource.Id.txtTypeAccount);
                txtAccountNumber = itemView.FindViewById<TextView>(Resource.Id.txtAccountNumber);
                txtAccountAmount = itemView.FindViewById<TextView>(Resource.Id.txtAccountAmount);

                itemView.Click += (sender, e) => listener(base.LayoutPosition);
            }
        }

        void OnClick(int position)
        {
            if (ItemClick != null)
                ItemClick(this, position);
        }
 
Back
Top Bottom