Home » Android » c# – ListView Creator called but no other methods

c# – ListView Creator called but no other methods

Posted by: admin May 14, 2020 Leave a comment

Questions:

This is my first time working with a ListView and I have had some trouble. I am sure I have a technique implemented incorrectly. However, after much searching on the internet and watching tutorials on list views I have not figured it out yet.

This will occasionally display, however, most of the time it is just not starting. When it does display it is when the screen is off and I run the app and turn the device screen on and it displayed the list. This is very hit and miss though.

The constructor is being called every time, however, after that Count and GetView are never called.

Everything seems to be displaying in my main.axml file below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android"
    p1:orientation="vertical"
    p1:layout_width="match_parent"
    p1:layout_height="match_parent"
    p1:id="@+id/linearLayout1">
    <Spinner
        p1:layout_width="match_parent"
        p1:layout_height="50.5dp"
        p1:id="@+id/stores"
        p1:layout_marginBottom="16.0dp" />
    <Button
        p1:id="@+id/scanItem"
        p1:layout_width="fill_parent"
        p1:layout_height="wrap_content"
        p1:text="Scan Item" />
    <ListView
        p1:minWidth="25px"
        p1:minHeight="25px"
        p1:layout_width="match_parent"
        p1:layout_height="match_parent"
        p1:id="@+id/itemView" />
</LinearLayout>

In my main activity I have traced everything through and everything is getting called.

To give you some background here is how I am creating the list I send over to the custom adapter I am using. I have a custom Object called RootObject that holds a list of Items

 var list = JsonConvert.DeserializeObject<RootObject>(response);
ListView myItems = FindViewById<ListView>(Resource.Id.itemView);
PIAdapter itemViewAdapter = new PIAdapter(this, list);
myItems.Adapter = itemViewAdapter;

This all seems to work

My Adapter Constructor is even being called and I can confirm 2 items are in my list.

However, when I include a Console.WriteLine in Count and GetView 99% of the time they are never called. Yet I can call all of the fields in the constructor and confirm that I have values filled in, and under certain conditions it does display properly.

public class PIAdapter : BaseAdapter
    {


        RootObject list = new RootObject();
        Activity context;

        public PIAdapter(Activity context, RootObject list)
        {
            this.list = list;
            this.context = context;
            Console.WriteLine("[My App] Step 10" + list.items.Count);

        }
        public override int Count
        {
            get
            {

                return list.items.Count;
            }
        }

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

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

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            Console.WriteLine("[My App] - Step 11");
            View view = convertView;
            if(view == null)
            {
                view = context.LayoutInflater.Inflate(Resource.Layout.myItem, null);
            }

            var item = list.items[position];

            ImageView customImage = view.FindViewById<ImageView>(Resource.Id.customImage);
            TextView customName = view.FindViewById<TextView>(Resource.Id.customName);
            TextView customBarcode = view.FindViewById<TextView>(Resource.Id.customBarcode);
            TextView customUp = view.FindViewById<TextView>(Resource.Id.customUpVote);
            TextView customDown = view.FindViewById<TextView>(Resource.Id.customDownVote);

            customName.Text = item.name;
            customBarcode.Text = item.barcode;
            customUp.Text = item.upvotes;
            customDown.Text = item.downvotes;

            //Koush.UrlImageViewHelper.SetUrlDrawable(customImage, "http://api.myurl.com/images/" + item.barcode + ".png", Resource.Drawable.myicon);

            return view;
        }


    }
}

In case it is needed I am editing this to include the myItem.axml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relativeLayout1">
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/customImage" />
        <TextView
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/customImage"
            android:id="@+id/customBarcode" />
        <TextView
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/customBarcode"
            android:id="@+id/customName"
            android:layout_toRightOf="@id/customImage" />
        <ImageView
            android:src="@drawable/up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/customImage"
            android:id="@+id/customUp"
            android:layout_below="@id/customName" />
        <TextView
            android:text="0"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/customUp"
            android:id="@+id/customUpVote"
            android:layout_below="@id/customName" />
        <ImageView
            android:src="@drawable/down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/customUpVote"
            android:id="@+id/customDown"
            android:layout_below="@id/customName" />
        <TextView
            android:text="0"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/customDown"
            android:id="@+id/customDownVote"
            android:layout_below="@id/customName" />
    </RelativeLayout>
</LinearLayout>
How to&Answers:

Pasted your code and tested in my device and emulator.
I dind’t have the problem you talked about.

But i’ve changed some things.

This is my Activity code:

var list = JsonConvert.DeserializeObject<RootObject>("{\"items\":[{\"barcode\": \"690443240066\",\"name\": \"gjjnn\",\"upvotes\": \"1\",\"downvotes\": \"0\",\"updated\": \"1466479409028\"},{\"barcode\": \"038000845031\",\"name\": \"fhj\",\"upvotes\": \"2\",\"downvotes\": \"1\",\"updated\": \"1466396732038\"}]}");
ListView myItems = FindViewById<ListView>(Resource.Id.itemView);
PIAdapter itemViewAdapter = new PIAdapter(this, list);
myItems.Adapter = itemViewAdapter;

And changed this in the main layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android"
    p1:orientation="vertical"
    p1:layout_width="match_parent"
    p1:layout_height="match_parent"
    p1:id="@+id/linearLayout1">
    <Spinner
        p1:layout_width="match_parent"
        p1:layout_height="50dp"
        p1:id="@+id/stores"
        p1:layout_marginBottom="16dp" />
    <Button
        p1:id="@+id/scanItem"
        p1:layout_width="fill_parent"
        p1:layout_height="wrap_content"
        p1:text="Scan Item" />
    <ListView
        p1:layout_width="match_parent"
        p1:layout_height="match_parent"
        p1:id="@+id/itemView" />
</LinearLayout>