0

I have a question about the ListView in android. I have added a list of 30 items which means that I need to scroll down. I use the adapter like below:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#70292929"
        android:layout_gravity="top|right"
        android:orientation="horizontal">

      <TableLayout
          android:id="@+id/tablelayout1"
          android:layout_gravity="center_horizontal"
          android:layout_weight="1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
        <TableRow android:gravity="left">

          <ListView
              android:id="@+id/listview1"
              android:background="#1b4f72"
              android:layout_weight="0.5"
              android:layout_gravity="top|left"
              android:layout_width="10px"
              android:layout_height="match_parent">
          </ListView>

        </TableRow>
        </TableLayout>
    </LinearLayout> 



            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                this.RequestWindowFeature(Android.Views.WindowFeatures.NoTitle);
                SetContentView(Resource.Layout.Main);
                LayoutInflater.Inflate(Resource.Layout.MENU, null);
                Android.Views.View globalMENUVIEW = LayoutInflater.Inflate(Resource.Layout.MENU, null); //Listview layout

                AlertDialog globalBUILDER = new AlertDialog.Builder(this).Create();
                globalBUILDER.Window.SetBackgroundDrawableResource(Resource.Drawable.transparant);
                globalBUILDER.Window.SetGravity(Android.Views.GravityFlags.Top | Android.Views.GravityFlags.Right);
                globalBUILDER.SetView(globalMENUVIEW);
                globalBUILDER.SetCanceledOnTouchOutside(true);
                globalBUILDER.Window.SetLayout(800, 500);
                globalBUILDER.Show();

                globalLISTVIEW1 = globalMENUVIEW.FindViewById<ListView>(Resource.Id.listview1);



                List<String> mainMENU = new List<String>();
                for (int i = 0; i < 30; i++)
                {
                    mainMENU.Add("hello" + i);
                }
                globalLISTVIEW1.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mainMENU.ToArray());

//Set item 5 as orange background
                for (int i = 0; i < globalLISTVIEW1.ChildCount; i++)
                {
                    if (i == 5) { globalLISTVIEW1.GetChildAt(i).SetBackgroundColor(Android.Graphics.Color.Orange); }
                    else { globalLISTVIEW1.GetChildAt(i).SetBackgroundColor(Android.Graphics.Color.Transparent); }
                }
            }

Now I have set a background color on itemindex 0. However when I scroll down, another item has the background color aswell.

I have red that the ListView use recycling of the items which cause this behaviour.

Now to my actual question. In my case I will never use more than 30 items and have red that it is possible to disable recycling by adding the below values to the Adapter.

But I do not know how to actually do this and where I will put this code exactly?

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}
Andreas
  • 1,121
  • 4
  • 17
  • 34
  • `ListView` doesn't recycle its views automatically, `RecyclerView` does. If you are using `ListView` can you post here the code where you are setting the background? – Giorgio Antonioli Oct 21 '19 at 22:44
  • @Giorgio I have updated my question with more code how I set the background. I set item index 5 as orange and the rest as transparent. – Andreas Oct 21 '19 at 23:19
  • Can you also post your adapter code? The problem there is related to the usage of `getChildCount()` – Giorgio Antonioli Oct 21 '19 at 23:34
  • I think this is what I don't understand when I have googled around. The only code I have is this one for the adapter: globalLISTVIEW1.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, mainMENU.ToArray()); – Andreas Oct 21 '19 at 23:42
  • Okay, interesting. So getChildCount() can be the problem somehow. I am a bit lost of how to do this to be honest. I don't get wiser when googling around on this problem. – Andreas Oct 21 '19 at 23:44
  • You can extend ArrayAdapter and use a custom layout. Inside the adapter you can change the background with the logic you wrote in the Activity, – Giorgio Antonioli Oct 21 '19 at 23:45
  • Yes, this is what I have seen when googled around. Many examples of extending the ArrayAdapter. But here I am completely stuck and then I posted my question :) My compiler don't like the "extends" and "ArrayAdapter" in for example: public class CustomAdapter extends ArrayAdapter. I don't know where to put the code that I suppose to put and all code I find on google has alot of syntax that my compiler doesn't like. I think I need some kind of example? – Andreas Oct 21 '19 at 23:52
  • I found that it was possible to add an Adapter to the solution. So I think I have succeed to modify it. So now I have created: `Adapter1 adapter1 = new Adapter1(context);` But are not sure how to assign the adapter1 to: `globalLISTVIEW1.SetAdapter(adapter1);` For this code I do get the compiler error: "AbsListView.SetAdapter(IListAdapter) is obselete: Please use the Adapter property setter". I am not sure if I am on the right track or how to set this Adapter? – Andreas Oct 22 '19 at 00:19

1 Answers1

1

This is how my Adapter looks like in code. I have added a "New Item" and choosen Adapter as the item which produces this code below.

As seen I have put the override for: getItemId and getItemViewType which should prevent recycling. But this code doesn't seem to be complete to use.

I still need to be able to pass in all those arguments as in the original adapter:

globalLISTVIEW1.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mainMENU.ToArray());

So how can we complete the below adapter?

 
    class Adapter1 : BaseAdapter
    {

        Context context;

        public Adapter1(Context context)
        {
            
            this.context = context;
        }


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

        public override long GetItemId(int position)
        {
            return 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>();
                //replace with your item and your holder items
                //comment back in
                //view = inflater.Inflate(Resource.Layout.item, parent, false);
                //holder.Title = view.FindViewById<TextView>(Resource.Id.text);
                view.Tag = holder;
            }


            //fill in your items
            //holder.Title.Text = "new text here";

            return view;
        }

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

    }

    class Adapter1ViewHolder : Java.Lang.Object
    {
        //Your adapter views to re-use
        //public TextView Title { get; set; }
    }
Andreas
  • 1,121
  • 4
  • 17
  • 34