0

I am trying to create my own Custom Adapter.

The goal is to call the adapter like this:

List<String> itemLIST = new List<String>(); itemLIST.Add("Hello World");
Adapter1 adapter1 = new Adapter1(context, Android.Resource.Layout.SimpleListItem1, itemLIST);

I now wonder how to construct the: "public Adapter1(....)" I beleive I miss something out in my attempt. The below code is just an extract of the whole adapter.

How do I complete this code correctly?

    class Adapter1 : BaseAdapter
    {

        Context context;
        int item = Android.Resource.Layout.SimpleListItem1;
        List<String> list1 = new List<String>();

        public Adapter1(Context context, int resource, List<String> itemArray)
        {
            list1 = itemArray;
            item = resource;
            this.context = context;
        }
    }
Andreas
  • 1,121
  • 4
  • 17
  • 34
  • Are you working in java or kotlin? Your `Adapter1 : BaseAdapter` looks like kotlin, but you're also using the `new` keyword and semicolons – Ben P. Oct 22 '19 at 20:07

1 Answers1

0

Thanks 0X0nosugar, I have succeeded with the help of your guide.

The Adapter is filled with items from itemLIST.

However, there is a problem when I scroll up and down. The items seems to randomly change places. Why does this happen?

                List<String> itemLIST = new List<String>(); 
                for (int i = 0; i < 15; i++)
                {
                    itemLIST.Add("hello" + i); 
                }
                Adapter1 adapter1 = new Adapter1(this, Android.Resource.Layout.SimpleListItem1, itemLIST);
                listView1.Adapter = adapter1;

Code for the adapter1:

  class Adapter1 : BaseAdapter
    {

        Context context;
        int item = 0;
        List<String> items = new List<String>();

        public Adapter1(Context context, int resource, List<String> itemArray)
        {
            items = itemArray;
            item = resource;
            this.context = context;
        }
   

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }
        //public override int getItemCount()
        //{
        //    return items.Count(); //returns total of items in the list
        //}
        public String getItem(int position)
        {
            return items[position]; //returns list item at the specified position
        }
        public override int GetItemViewType(int position)
        {
            return position;
        }
        

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView;
            Adapter1ViewHolder holder = null;

            if (view != null)
                holder = view.Tag as Adapter1ViewHolder;

            if (holder == null)
            {
                holder = new Adapter1ViewHolder();
                var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();

                view = inflater.Inflate(Resource.Layout.textview, parent, false);
                String currentItem = getItem(position);

                //get the TextView for item name and item description
                TextView textViewItemName = view.FindViewById<TextView>(Resource.Id.itemtext);                    

                //sets the text for item name and item description from the current item object
                textViewItemName.SetText(currentItem, TextView.BufferType.Normal);

                holder.Title = textViewItemName;
                view.Tag = holder;
            }
            return view;
        }

        //Fill in cound here, currently 0
        public override int Count
        {
            get
            {
                return items.Count;
            }
        }

    }

    class Adapter1ViewHolder : Java.Lang.Object
    {
        //Your adapter views to re-use
        public TextView Title { get; set; }
    }
Andreas
  • 1,121
  • 4
  • 17
  • 34
  • The above adapter almost works. It displays the items in the listView1 but when scrolling the items changes places randomly. Why does this happen? – Andreas Oct 22 '19 at 22:39
  • I removed this and now it works: `if (view != null) holder = view.Tag as Adapter1ViewHolder;` – Andreas Oct 22 '19 at 23:27